diff --git a/client/src/test/java/org/apache/rocketmq/client/consumer/store/LocalFileOffsetStoreTest.java b/client/src/test/java/org/apache/rocketmq/client/consumer/store/LocalFileOffsetStoreTest.java index 0a54855bafc3d7e14523cf01f247072136b586f1..0b522e60e26a8740c2767c8d1422ae8dc2729623 100644 --- a/client/src/test/java/org/apache/rocketmq/client/consumer/store/LocalFileOffsetStoreTest.java +++ b/client/src/test/java/org/apache/rocketmq/client/consumer/store/LocalFileOffsetStoreTest.java @@ -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); } diff --git a/client/src/test/java/org/apache/rocketmq/client/consumer/store/RemoteBrokerOffsetStoreTest.java b/client/src/test/java/org/apache/rocketmq/client/consumer/store/RemoteBrokerOffsetStoreTest.java index 7ecb0225c4193e5d9c7fa7d02b9eefc3e5b97fa6..a13930fdea9755328152bacb541a4bc800a1fefc 100644 --- a/client/src/test/java/org/apache/rocketmq/client/consumer/store/RemoteBrokerOffsetStoreTest.java +++ b/client/src/test/java/org/apache/rocketmq/client/consumer/store/RemoteBrokerOffsetStoreTest.java @@ -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); } diff --git a/client/src/test/java/org/apache/rocketmq/client/impl/MQClientAPIImplTest.java b/client/src/test/java/org/apache/rocketmq/client/impl/MQClientAPIImplTest.java index cbcf560127b6e1fda1b38a797c911089874aca4b..3553738f387f3d9c2f9bca4b4cd2c2ef2801801e 100644 --- a/client/src/test/java/org/apache/rocketmq/client/impl/MQClientAPIImplTest.java +++ b/client/src/test/java/org/apache/rocketmq/client/impl/MQClientAPIImplTest.java @@ -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); diff --git a/client/src/test/java/org/apache/rocketmq/client/impl/factory/MQClientInstanceTest.java b/client/src/test/java/org/apache/rocketmq/client/impl/factory/MQClientInstanceTest.java index b7b07c6af99722bee54ec280cf28ec3649f60e67..c32cdab65c47b79263708a2edc804c1baf03e735 100644 --- a/client/src/test/java/org/apache/rocketmq/client/impl/factory/MQClientInstanceTest.java +++ b/client/src/test/java/org/apache/rocketmq/client/impl/factory/MQClientInstanceTest.java @@ -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";