提交 4dd6c385 编写于 作者: Y yukon

[ROCKETMQ-52] Use MockitoJUnitRunner with Mock

上级 8c933f2c
......@@ -21,25 +21,26 @@ import java.util.HashSet;
import org.apache.rocketmq.client.ClientConfig;
import org.apache.rocketmq.client.impl.factory.MQClientInstance;
import org.apache.rocketmq.common.message.MessageQueue;
import org.junit.BeforeClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class LocalFileOffsetStoreTest {
@Mock
private static MQClientInstance mQClientFactory;
private static String group = "FooBarGroup";
private static String topic = "FooBar";
private static String brokerName = "DefaultBrokerName";
private MQClientInstance mQClientFactory;
private String group = "FooBarGroup";
private String topic = "FooBar";
private String brokerName = "DefaultBrokerName";
@BeforeClass
public static void init() {
@Before
public void init() {
System.setProperty("rocketmq.client.localOffsetStoreDir", System.getProperty("java.io.tmpdir") + ".rocketmq_offsets");
mQClientFactory = Mockito.mock(MQClientInstance.class);
String clientId = new ClientConfig().buildMQClientId() + "#TestNamespace" + System.currentTimeMillis();
when(mQClientFactory.getClientId()).thenReturn(clientId);
}
......
......@@ -27,10 +27,12 @@ import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.common.protocol.header.QueryConsumerOffsetRequestHeader;
import org.apache.rocketmq.common.protocol.header.UpdateConsumerOffsetRequestHeader;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.junit.BeforeClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -39,27 +41,24 @@ import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class RemoteBrokerOffsetStoreTest {
@Mock
private static MQClientInstance mQClientFactory;
private MQClientInstance mQClientFactory;
@Mock
private static MQClientAPIImpl mqClientAPI;
private static String group = "FooBarGroup";
private static String topic = "FooBar";
private static String brokerName = "DefaultBrokerName";
private MQClientAPIImpl mqClientAPI;
private String group = "FooBarGroup";
private String topic = "FooBar";
private String brokerName = "DefaultBrokerName";
@BeforeClass
public static void init() {
@Before
public void init() {
System.setProperty("rocketmq.client.localOffsetStoreDir", System.getProperty("java.io.tmpdir") + ".rocketmq_offsets");
mQClientFactory = mock(MQClientInstance.class);
String clientId = new ClientConfig().buildMQClientId() + "#TestNamespace" + System.currentTimeMillis();
when(mQClientFactory.getClientId()).thenReturn(clientId);
when(mQClientFactory.findBrokerAddressInAdmin(brokerName)).thenReturn(new FindBrokerResult("127.0.0.1", false));
mqClientAPI = mock(MQClientAPIImpl.class);
when(mQClientFactory.getMQClientAPIImpl()).thenReturn(mqClientAPI);
}
......
......@@ -35,13 +35,14 @@ import org.apache.rocketmq.remoting.RemotingClient;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.apache.rocketmq.remoting.exception.RemotingTimeoutException;
import org.apache.rocketmq.remoting.netty.NettyClientConfig;
import org.apache.rocketmq.remoting.netty.NettyRemotingClient;
import org.apache.rocketmq.remoting.netty.ResponseFuture;
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
import org.junit.BeforeClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -52,14 +53,14 @@ import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
@RunWith(MockitoJUnitRunner.class)
public class MQClientAPIImplTest {
private static MQClientAPIImpl mqClientAPI = new MQClientAPIImpl(new NettyClientConfig(), null, null, new ClientConfig());
private MQClientAPIImpl mqClientAPI = new MQClientAPIImpl(new NettyClientConfig(), null, null, new ClientConfig());
@Mock
private static RemotingClient remotingClient;
private RemotingClient remotingClient;
@Mock
private static DefaultMQProducerImpl defaultMQProducerImpl;
private DefaultMQProducerImpl defaultMQProducerImpl;
private String brokerAddr = "127.0.0.1";
private String brokerName = "DefaultBroker";
......@@ -67,10 +68,8 @@ public class MQClientAPIImplTest {
private static String topic = "FooBar";
private Message msg = new Message("FooBar", new byte[] {});
@BeforeClass
public static void init() throws Exception {
remotingClient = mock(NettyRemotingClient.class);
defaultMQProducerImpl = mock(DefaultMQProducerImpl.class);
@Before
public void init() throws Exception {
Field field = MQClientAPIImpl.class.getDeclaredField("remotingClient");
field.setAccessible(true);
field.set(mqClientAPI, remotingClient);
......
......@@ -32,7 +32,6 @@ import org.apache.rocketmq.common.protocol.route.TopicRouteData;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -40,9 +39,7 @@ import static org.mockito.Mockito.mock;
@RunWith(MockitoJUnitRunner.class)
public class MQClientInstanceTest {
@InjectMocks
private MQClientInstance mqClientInstance = MQClientManager.getInstance().getAndCreateMQClientInstance(new ClientConfig());;
private String topic = "FooBar";
private String group = "FooBarGroup";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册