提交 bbcb69ec 编写于 作者: Y yukon

[ROCKETMQ-52] Use MockitoJUnitRunner with Mock

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