diff --git a/src/test/java/com/hivemq/bootstrap/BindInformationTest.java b/src/test/java/com/hivemq/bootstrap/BindInformationTest.java index 6ba2d82fa..06736ad0b 100644 --- a/src/test/java/com/hivemq/bootstrap/BindInformationTest.java +++ b/src/test/java/com/hivemq/bootstrap/BindInformationTest.java @@ -17,6 +17,7 @@ import com.hivemq.configuration.service.entity.TcpListener; import io.netty.channel.ChannelFuture; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -31,13 +32,19 @@ public class BindInformationTest { ChannelFuture future; private TcpListener listener; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); listener = new TcpListener(1883, "0.0.0.0"); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_bind_information() throws Exception { final BindInformation bindInformation = new BindInformation(listener, future); @@ -55,4 +62,4 @@ public void test_listener_null() throws Exception { public void test_future_null() throws Exception { new BindInformation(listener, null); } -} \ No newline at end of file +} diff --git a/src/test/java/com/hivemq/bootstrap/HiveMQNettyBootstrapTest.java b/src/test/java/com/hivemq/bootstrap/HiveMQNettyBootstrapTest.java index 5669cce03..f4f3d4464 100644 --- a/src/test/java/com/hivemq/bootstrap/HiveMQNettyBootstrapTest.java +++ b/src/test/java/com/hivemq/bootstrap/HiveMQNettyBootstrapTest.java @@ -32,6 +32,7 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -52,6 +53,7 @@ public class HiveMQNettyBootstrapTest { private HiveMQNettyBootstrap hiveMQNettyBootstrap; + private AutoCloseable closeable; @Mock private ShutdownHooks shutdownHooks; @@ -73,7 +75,7 @@ public class HiveMQNettyBootstrapTest { @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); hiveMQNettyBootstrap = new HiveMQNettyBootstrap(shutdownHooks, listenerConfigurationService, channelInitializerFactoryImpl, @@ -87,6 +89,11 @@ public void before() { abstractChannelInitializer); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void bootstrapServer_whenNoListenersProvided_thenSuccessfulBootstrap() { diff --git a/src/test/java/com/hivemq/bootstrap/netty/ChannelDependenciesTest.java b/src/test/java/com/hivemq/bootstrap/netty/ChannelDependenciesTest.java index 1358c29e1..e10d4b599 100644 --- a/src/test/java/com/hivemq/bootstrap/netty/ChannelDependenciesTest.java +++ b/src/test/java/com/hivemq/bootstrap/netty/ChannelDependenciesTest.java @@ -45,6 +45,7 @@ import com.hivemq.security.ssl.SslParameterHandler; import io.netty.channel.group.ChannelGroup; import io.netty.handler.traffic.GlobalTrafficShapingHandler; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -145,10 +146,11 @@ public class ChannelDependenciesTest { private @NotNull ShutdownHooks shutdownHooks; private @NotNull ChannelDependencies channelDependencies; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); channelDependencies = new ChannelDependencies(noConnectIdleHandler, () -> connectHandler, @@ -182,6 +184,11 @@ public void setUp() throws Exception { shutdownHooks); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void getters_returnAllHandlers() { assertNotNull(channelDependencies.getNoConnectIdleHandler()); diff --git a/src/test/java/com/hivemq/bootstrap/netty/ExceptionHandlerTest.java b/src/test/java/com/hivemq/bootstrap/netty/ExceptionHandlerTest.java index 3bd9ae526..910dfcfe9 100644 --- a/src/test/java/com/hivemq/bootstrap/netty/ExceptionHandlerTest.java +++ b/src/test/java/com/hivemq/bootstrap/netty/ExceptionHandlerTest.java @@ -22,6 +22,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPipeline; import io.netty.handler.codec.CorruptedFrameException; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -57,10 +58,11 @@ public class ExceptionHandlerTest { ClientConnection clientConnection; private ExceptionHandler handler; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(ctx.pipeline()).thenReturn(pipeline); when(channel.pipeline()).thenReturn(pipeline); @@ -72,6 +74,11 @@ public void before() { handler = new ExceptionHandler(mqttServerDisconnector); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_SSLException() throws Exception { diff --git a/src/test/java/com/hivemq/bootstrap/netty/initializer/ChannelGroupHandlerTest.java b/src/test/java/com/hivemq/bootstrap/netty/initializer/ChannelGroupHandlerTest.java index 3177671b7..2b2ab2875 100644 --- a/src/test/java/com/hivemq/bootstrap/netty/initializer/ChannelGroupHandlerTest.java +++ b/src/test/java/com/hivemq/bootstrap/netty/initializer/ChannelGroupHandlerTest.java @@ -19,6 +19,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPipeline; import io.netty.channel.group.ChannelGroup; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -44,16 +45,22 @@ public class ChannelGroupHandlerTest { ChannelPipeline channelPipeline; private ChannelGroupHandler channelGroupHandler; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); channelGroupHandler = new ChannelGroupHandler(channelGroup); when(ctx.channel()).thenReturn(channel); when(ctx.pipeline()).thenReturn(channelPipeline); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_channel_active() throws Exception { channelGroupHandler.channelActive(ctx); diff --git a/src/test/java/com/hivemq/bootstrap/netty/initializer/ChannelInitializerFactoryImplTest.java b/src/test/java/com/hivemq/bootstrap/netty/initializer/ChannelInitializerFactoryImplTest.java index 76865d84f..23d7c8a68 100644 --- a/src/test/java/com/hivemq/bootstrap/netty/initializer/ChannelInitializerFactoryImplTest.java +++ b/src/test/java/com/hivemq/bootstrap/netty/initializer/ChannelInitializerFactoryImplTest.java @@ -30,6 +30,7 @@ import com.hivemq.security.ssl.NonSslHandler; import com.hivemq.security.ssl.SslFactory; import io.netty.channel.Channel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -69,10 +70,11 @@ public class ChannelInitializerFactoryImplTest { private RestrictionsConfigurationService restrictionsConfigurationService; private ChannelInitializerFactoryImpl channelInitializerFactory; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(channelDependencies.getConfigurationService()).thenReturn(fullConfigurationService); when(channelDependencies.getRestrictionsConfigurationService()).thenReturn(restrictionsConfigurationService); when(restrictionsConfigurationService.incomingLimit()).thenReturn(0L); @@ -80,6 +82,11 @@ public void before() { new TestChannelInitializerFactory(channelDependencies, sslFactory, nonSslHandlerProvider); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_get_tcp_initializer() { final TcpListener tcpListener = new TcpListener(0, "0"); diff --git a/src/test/java/com/hivemq/bootstrap/netty/initializer/TcpChannelInitializerTest.java b/src/test/java/com/hivemq/bootstrap/netty/initializer/TcpChannelInitializerTest.java index a44c6d730..6db206975 100644 --- a/src/test/java/com/hivemq/bootstrap/netty/initializer/TcpChannelInitializerTest.java +++ b/src/test/java/com/hivemq/bootstrap/netty/initializer/TcpChannelInitializerTest.java @@ -24,6 +24,7 @@ import com.hivemq.security.ssl.NonSslHandler; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -58,14 +59,13 @@ public class TcpChannelInitializerTest { @Mock private RestrictionsConfigurationService restrictionsConfigurationService; - private ChannelPipeline pipeline; - private TcpChannelInitializer tcpChannelInitializer; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(channelDependencies.getConfigurationService()).thenReturn(fullConfigurationService); when(channelDependencies.getRestrictionsConfigurationService()).thenReturn(restrictionsConfigurationService); @@ -80,10 +80,15 @@ public void before() { } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_add_special_handlers() throws Exception { tcpChannelInitializer.addSpecialHandlers(socketChannel); assertEquals(NON_SSL_HANDLER, pipeline.names().get(0)); } -} \ No newline at end of file +} diff --git a/src/test/java/com/hivemq/bootstrap/netty/initializer/TlsTcpChannelInitializerTest.java b/src/test/java/com/hivemq/bootstrap/netty/initializer/TlsTcpChannelInitializerTest.java index 6ffd4236a..a13ea4bdd 100644 --- a/src/test/java/com/hivemq/bootstrap/netty/initializer/TlsTcpChannelInitializerTest.java +++ b/src/test/java/com/hivemq/bootstrap/netty/initializer/TlsTcpChannelInitializerTest.java @@ -34,6 +34,7 @@ import io.netty.util.Attribute; import io.netty.util.AttributeKey; import io.netty.util.concurrent.Future; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -90,10 +91,11 @@ public class TlsTcpChannelInitializerTest { private ChannelPipeline pipeline; private TlsTcpChannelInitializer tlstcpChannelInitializer; + private AutoCloseable closeable; @Before public void before() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); pipeline = new FakeChannelPipeline(); @@ -117,6 +119,11 @@ public void before() throws Exception { } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_add_special_handlers() { diff --git a/src/test/java/com/hivemq/bootstrap/netty/initializer/TlsWebsocketChannelInitializerTest.java b/src/test/java/com/hivemq/bootstrap/netty/initializer/TlsWebsocketChannelInitializerTest.java index 53e0565c6..3c9a0f564 100644 --- a/src/test/java/com/hivemq/bootstrap/netty/initializer/TlsWebsocketChannelInitializerTest.java +++ b/src/test/java/com/hivemq/bootstrap/netty/initializer/TlsWebsocketChannelInitializerTest.java @@ -34,6 +34,7 @@ import io.netty.util.Attribute; import io.netty.util.AttributeKey; import io.netty.util.concurrent.Future; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -95,10 +96,11 @@ public class TlsWebsocketChannelInitializerTest { private Tls tls; private ChannelPipeline pipeline; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); pipeline = new FakeChannelPipeline(); @@ -120,6 +122,11 @@ public void before() { when(channelDependencies.getMqttServerDisconnector()).thenReturn(mqttServerDisconnector); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_add_special_handlers() { diff --git a/src/test/java/com/hivemq/bootstrap/netty/initializer/WebsocketChannelInitializerTest.java b/src/test/java/com/hivemq/bootstrap/netty/initializer/WebsocketChannelInitializerTest.java index 1725c0a3b..cdc19b692 100644 --- a/src/test/java/com/hivemq/bootstrap/netty/initializer/WebsocketChannelInitializerTest.java +++ b/src/test/java/com/hivemq/bootstrap/netty/initializer/WebsocketChannelInitializerTest.java @@ -24,6 +24,7 @@ import com.hivemq.security.ssl.NonSslHandler; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -58,10 +59,11 @@ public class WebsocketChannelInitializerTest { private RestrictionsConfigurationService restrictionsConfigurationService; private ChannelPipeline pipeline; + private AutoCloseable closeable; @Before public void before() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); pipeline = new FakeChannelPipeline(); @@ -72,6 +74,11 @@ public void before() throws Exception { when(restrictionsConfigurationService.incomingLimit()).thenReturn(0L); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_add_special_handlers() throws Exception { diff --git a/src/test/java/com/hivemq/codec/decoder/MQTTMessageDecoderTest.java b/src/test/java/com/hivemq/codec/decoder/MQTTMessageDecoderTest.java index 28e1a0cf1..5673a3daf 100644 --- a/src/test/java/com/hivemq/codec/decoder/MQTTMessageDecoderTest.java +++ b/src/test/java/com/hivemq/codec/decoder/MQTTMessageDecoderTest.java @@ -26,6 +26,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -42,10 +43,11 @@ public class MQTTMessageDecoderTest { private @NotNull EmbeddedChannel channel; private @NotNull ClientConnection clientConnection; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); channel = new EmbeddedChannel(TestMqttDecoder.create()); clientConnection = new DummyClientConnection(channel, null); //setting version to fake "connected" state @@ -53,6 +55,11 @@ public void setUp() throws Exception { channel.attr(ClientConnectionContext.CHANNEL_ATTRIBUTE_NAME).set(clientConnection); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + /* *********************** * Test invalid messages * * ***********************/ diff --git a/src/test/java/com/hivemq/codec/decoder/Mqtt31ConnectDecoderTest.java b/src/test/java/com/hivemq/codec/decoder/Mqtt31ConnectDecoderTest.java index 510c62ddc..cb30fdb22 100644 --- a/src/test/java/com/hivemq/codec/decoder/Mqtt31ConnectDecoderTest.java +++ b/src/test/java/com/hivemq/codec/decoder/Mqtt31ConnectDecoderTest.java @@ -30,6 +30,7 @@ import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -59,17 +60,18 @@ public class Mqtt31ConnectDecoderTest { @Mock private @NotNull ChannelFuture channelFuture; - private @NotNull Mqtt31ConnectDecoder decoder; - private ClientConnection clientConnection; - @Mock private @NotNull MqttConnacker connacker; + private @NotNull Mqtt31ConnectDecoder decoder; + private ClientConnection clientConnection; + private AutoCloseable closeable; + private static final byte fixedHeader = 0b0001_0000; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(channel.writeAndFlush(any())).thenReturn(channelFuture); clientConnection = new DummyClientConnection(channel, null); @@ -84,6 +86,11 @@ public void setUp() throws Exception { new HivemqId()); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_decode_no_will_no_user_no_pw() { final ByteBuf buf = Unpooled.buffer(); diff --git a/src/test/java/com/hivemq/codec/decoder/Mqtt31ConnectDecoderValidationsTest.java b/src/test/java/com/hivemq/codec/decoder/Mqtt31ConnectDecoderValidationsTest.java index e6c78ff4f..42e36975b 100644 --- a/src/test/java/com/hivemq/codec/decoder/Mqtt31ConnectDecoderValidationsTest.java +++ b/src/test/java/com/hivemq/codec/decoder/Mqtt31ConnectDecoderValidationsTest.java @@ -30,6 +30,7 @@ import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -63,13 +64,14 @@ public class Mqtt31ConnectDecoderValidationsTest { private Mqtt31ConnectDecoder decoder; private ClientConnection clientConnection; + private AutoCloseable closeable; private static final byte fixedHeader = 0b0001_0000; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(fullConfigurationService.mqttConfiguration()).thenReturn(mqttConfigurationService); decoder = new Mqtt31ConnectDecoder(connacker, new ClientIds(new HivemqId()), @@ -81,6 +83,11 @@ public void setUp() throws Exception { clientConnection)); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_invalid_header() { diff --git a/src/test/java/com/hivemq/codec/decoder/Mqtt3DisconnectDecoderTest.java b/src/test/java/com/hivemq/codec/decoder/Mqtt3DisconnectDecoderTest.java index efdba369a..79c74c581 100644 --- a/src/test/java/com/hivemq/codec/decoder/Mqtt3DisconnectDecoderTest.java +++ b/src/test/java/com/hivemq/codec/decoder/Mqtt3DisconnectDecoderTest.java @@ -23,6 +23,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -36,16 +37,22 @@ public class Mqtt3DisconnectDecoderTest { private @NotNull EmbeddedChannel channel; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); channel = new EmbeddedChannel(TestMqttDecoder.create()); channel.attr(ClientConnectionContext.CHANNEL_ATTRIBUTE_NAME).set(new DummyClientConnection(channel, null)); ClientConnection.of(channel).setProtocolVersion(ProtocolVersion.MQTTv3_1_1); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_disconnect_received() { final ByteBuf buf = Unpooled.buffer(); diff --git a/src/test/java/com/hivemq/codec/decoder/Mqtt3PubackDecoderTest.java b/src/test/java/com/hivemq/codec/decoder/Mqtt3PubackDecoderTest.java index 5f98a63e9..b40bb7afa 100644 --- a/src/test/java/com/hivemq/codec/decoder/Mqtt3PubackDecoderTest.java +++ b/src/test/java/com/hivemq/codec/decoder/Mqtt3PubackDecoderTest.java @@ -23,6 +23,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -36,16 +37,22 @@ public class Mqtt3PubackDecoderTest { private @NotNull EmbeddedChannel channel; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); channel = new EmbeddedChannel(TestMqttDecoder.create()); channel.attr(ClientConnectionContext.CHANNEL_ATTRIBUTE_NAME).set(new DummyClientConnection(channel, null)); ClientConnection.of(channel).setProtocolVersion(ProtocolVersion.MQTTv3_1_1); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_puback_received() { diff --git a/src/test/java/com/hivemq/codec/decoder/Mqtt3PubcompDecoderTest.java b/src/test/java/com/hivemq/codec/decoder/Mqtt3PubcompDecoderTest.java index 75322af1d..bf4de9d49 100644 --- a/src/test/java/com/hivemq/codec/decoder/Mqtt3PubcompDecoderTest.java +++ b/src/test/java/com/hivemq/codec/decoder/Mqtt3PubcompDecoderTest.java @@ -23,6 +23,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -37,15 +38,21 @@ public class Mqtt3PubcompDecoderTest { private @NotNull EmbeddedChannel channel; private @NotNull ClientConnection clientConnection; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); channel = new EmbeddedChannel(TestMqttDecoder.create()); clientConnection = new DummyClientConnection(channel, null); channel.attr(ClientConnectionContext.CHANNEL_ATTRIBUTE_NAME).set(clientConnection); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_pubcomp_received() { clientConnection.setProtocolVersion(ProtocolVersion.MQTTv3_1_1); diff --git a/src/test/java/com/hivemq/codec/decoder/Mqtt3PublishDecoderTest.java b/src/test/java/com/hivemq/codec/decoder/Mqtt3PublishDecoderTest.java index dad196a3d..89e9ef762 100644 --- a/src/test/java/com/hivemq/codec/decoder/Mqtt3PublishDecoderTest.java +++ b/src/test/java/com/hivemq/codec/decoder/Mqtt3PublishDecoderTest.java @@ -26,6 +26,7 @@ import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; import org.apache.commons.lang3.RandomStringUtils; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -45,16 +46,22 @@ public class Mqtt3PublishDecoderTest { private EmbeddedChannel channel; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); channel = new EmbeddedChannel(TestMqttDecoder.create()); channel.attr(ClientConnectionContext.CHANNEL_ATTRIBUTE_NAME).set(new DummyClientConnection(channel, null)); ClientConnection.of(channel).setProtocolVersion(ProtocolVersion.MQTTv3_1_1); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_valid_pub_qos_0() { diff --git a/src/test/java/com/hivemq/codec/decoder/Mqtt3PubrecDecoderTest.java b/src/test/java/com/hivemq/codec/decoder/Mqtt3PubrecDecoderTest.java index 527a0f667..09f5a154a 100644 --- a/src/test/java/com/hivemq/codec/decoder/Mqtt3PubrecDecoderTest.java +++ b/src/test/java/com/hivemq/codec/decoder/Mqtt3PubrecDecoderTest.java @@ -23,6 +23,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -36,13 +37,19 @@ public class Mqtt3PubrecDecoderTest { private @NotNull EmbeddedChannel channel; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); channel = new EmbeddedChannel(TestMqttDecoder.create()); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_pubrec_received() { diff --git a/src/test/java/com/hivemq/codec/decoder/Mqtt3PubrelDecoderTest.java b/src/test/java/com/hivemq/codec/decoder/Mqtt3PubrelDecoderTest.java index b8ac0a1e1..c76bd3304 100644 --- a/src/test/java/com/hivemq/codec/decoder/Mqtt3PubrelDecoderTest.java +++ b/src/test/java/com/hivemq/codec/decoder/Mqtt3PubrelDecoderTest.java @@ -23,6 +23,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -36,13 +37,19 @@ public class Mqtt3PubrelDecoderTest { private @NotNull EmbeddedChannel channel; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); channel = new EmbeddedChannel(TestMqttDecoder.create()); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_pubrel_received() { diff --git a/src/test/java/com/hivemq/codec/decoder/Mqtt3SubscribeDecoderTest.java b/src/test/java/com/hivemq/codec/decoder/Mqtt3SubscribeDecoderTest.java index 5b4742101..d69ee532e 100644 --- a/src/test/java/com/hivemq/codec/decoder/Mqtt3SubscribeDecoderTest.java +++ b/src/test/java/com/hivemq/codec/decoder/Mqtt3SubscribeDecoderTest.java @@ -24,6 +24,7 @@ import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; import org.apache.commons.lang3.RandomStringUtils; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -39,16 +40,22 @@ public class Mqtt3SubscribeDecoderTest { private @NotNull EmbeddedChannel channel; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); channel = new EmbeddedChannel(TestMqttDecoder.create()); channel.attr(ClientConnectionContext.CHANNEL_ATTRIBUTE_NAME).set(new DummyClientConnection(channel, null)); ClientConnection.of(channel).setProtocolVersion(ProtocolVersion.MQTTv3_1_1); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_subscribe_received() { diff --git a/src/test/java/com/hivemq/codec/decoder/Mqtt3UnsubscribeDecoderTest.java b/src/test/java/com/hivemq/codec/decoder/Mqtt3UnsubscribeDecoderTest.java index ea5f25654..41a6811e1 100644 --- a/src/test/java/com/hivemq/codec/decoder/Mqtt3UnsubscribeDecoderTest.java +++ b/src/test/java/com/hivemq/codec/decoder/Mqtt3UnsubscribeDecoderTest.java @@ -23,6 +23,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -38,16 +39,22 @@ public class Mqtt3UnsubscribeDecoderTest { private @NotNull EmbeddedChannel channel; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); channel = new EmbeddedChannel(TestMqttDecoder.create()); channel.attr(ClientConnectionContext.CHANNEL_ATTRIBUTE_NAME).set(new DummyClientConnection(channel, null)); ClientConnection.of(channel).setProtocolVersion(ProtocolVersion.MQTTv3_1_1); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_unsubscribe_received() { diff --git a/src/test/java/com/hivemq/codec/decoder/MqttPingreqDecoderTest.java b/src/test/java/com/hivemq/codec/decoder/MqttPingreqDecoderTest.java index 91ba17907..e41d5ddc6 100644 --- a/src/test/java/com/hivemq/codec/decoder/MqttPingreqDecoderTest.java +++ b/src/test/java/com/hivemq/codec/decoder/MqttPingreqDecoderTest.java @@ -23,6 +23,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -38,14 +39,20 @@ public class MqttPingreqDecoderTest { private @NotNull EmbeddedChannel channel; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); channel = new EmbeddedChannel(TestMqttDecoder.create()); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_ping_request_received_mqtt_311() { @@ -131,4 +138,4 @@ public void test_ping_request_invalid_header_ignored_mqtt_31() { assertTrue(channel.isActive()); } -} \ No newline at end of file +} diff --git a/src/test/java/com/hivemq/codec/decoder/mqtt311/Mqtt311ConnectDecoderTest.java b/src/test/java/com/hivemq/codec/decoder/mqtt311/Mqtt311ConnectDecoderTest.java index 0ac4b2156..2d0b5f1d0 100644 --- a/src/test/java/com/hivemq/codec/decoder/mqtt311/Mqtt311ConnectDecoderTest.java +++ b/src/test/java/com/hivemq/codec/decoder/mqtt311/Mqtt311ConnectDecoderTest.java @@ -33,6 +33,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import org.apache.commons.lang3.RandomStringUtils; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -75,11 +76,12 @@ public class Mqtt311ConnectDecoderTest { private static final byte fixedHeader = 0b0001_0000; private HivemqId hiveMQId; private ClientConnection clientConnection; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); clientConnection = new DummyClientConnection(channel, null); when(channel.attr(ClientConnectionContext.CHANNEL_ATTRIBUTE_NAME)).thenReturn(new TestChannelAttribute<>( @@ -95,6 +97,11 @@ public void setUp() throws Exception { hiveMQId); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_decode_no_will_no_user_no_pw() { final ByteBuf buf = Unpooled.buffer(); diff --git a/src/test/java/com/hivemq/codec/decoder/mqtt311/Mqtt311ConnectDecoderValidationsTest.java b/src/test/java/com/hivemq/codec/decoder/mqtt311/Mqtt311ConnectDecoderValidationsTest.java index aeb443ebb..852562d2e 100644 --- a/src/test/java/com/hivemq/codec/decoder/mqtt311/Mqtt311ConnectDecoderValidationsTest.java +++ b/src/test/java/com/hivemq/codec/decoder/mqtt311/Mqtt311ConnectDecoderValidationsTest.java @@ -28,6 +28,7 @@ import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -56,13 +57,14 @@ public class Mqtt311ConnectDecoderValidationsTest { private MqttConnacker connacker; private ClientConnection clientConnection; private Mqtt311ConnectDecoder decoder; + private AutoCloseable closeable; private static final byte fixedHeader = 0b0001_0000; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); clientConnection = new DummyClientConnection(channel, null); when(channel.attr(ClientConnectionContext.CHANNEL_ATTRIBUTE_NAME)).thenReturn(new TestChannelAttribute<>(new DummyClientConnection( @@ -75,6 +77,11 @@ public void setUp() throws Exception { new HivemqId()); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_invalid_protocol_name_mqtt_invalid_case() { diff --git a/src/test/java/com/hivemq/codec/decoder/mqtt5/Mqtt5DisconnectDecoderTest.java b/src/test/java/com/hivemq/codec/decoder/mqtt5/Mqtt5DisconnectDecoderTest.java index 89b7410bc..b7784531a 100644 --- a/src/test/java/com/hivemq/codec/decoder/mqtt5/Mqtt5DisconnectDecoderTest.java +++ b/src/test/java/com/hivemq/codec/decoder/mqtt5/Mqtt5DisconnectDecoderTest.java @@ -29,6 +29,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -55,13 +56,20 @@ public class Mqtt5DisconnectDecoderTest extends AbstractMqtt5DecoderTest { @Mock private @NotNull SecurityConfigurationService securityConfigurationService; + private AutoCloseable closeable; + @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(securityConfigurationService.allowRequestProblemInformation()).thenReturn(true); ClientConnection.of(channel).setClientSessionExpiryInterval(100L); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void decode_big_packet() { final byte[] encoded = { diff --git a/src/test/java/com/hivemq/configuration/ioc/ConfigurationFileProviderTest.java b/src/test/java/com/hivemq/configuration/ioc/ConfigurationFileProviderTest.java index de22d0f34..9b2bc08da 100644 --- a/src/test/java/com/hivemq/configuration/ioc/ConfigurationFileProviderTest.java +++ b/src/test/java/com/hivemq/configuration/ioc/ConfigurationFileProviderTest.java @@ -61,11 +61,12 @@ public class ConfigurationFileProviderTest { private File confFolder; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); logger.addAppender(mockAppender); @@ -86,6 +87,11 @@ public void tearDown() throws Exception { logger.detachAppender(mockAppender); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_conf_file_ok() throws Exception { diff --git a/src/test/java/com/hivemq/configuration/reader/AbstractConfigurationTest.java b/src/test/java/com/hivemq/configuration/reader/AbstractConfigurationTest.java index 20fb0f811..d1f789cb1 100644 --- a/src/test/java/com/hivemq/configuration/reader/AbstractConfigurationTest.java +++ b/src/test/java/com/hivemq/configuration/reader/AbstractConfigurationTest.java @@ -30,6 +30,7 @@ import com.hivemq.statistics.UsageStatisticsConfig; import com.hivemq.statistics.UsageStatisticsConfigImpl; import com.hivemq.util.EnvVarUtil; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.rules.TemporaryFolder; @@ -59,9 +60,11 @@ public class AbstractConfigurationTest { SystemInformation systemInformation; PersistenceConfigurationService persistenceConfigurationService; + private AutoCloseable closeable; + @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); listenerConfigurationService = new ListenerConfigurationServiceImpl(); xmlFile = temporaryFolder.newFile(); @@ -84,4 +87,9 @@ public void setUp() throws Exception { new PersistenceConfigurator(persistenceConfigurationService)); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + } diff --git a/src/test/java/com/hivemq/configuration/reader/ConfigFileReaderTest.java b/src/test/java/com/hivemq/configuration/reader/ConfigFileReaderTest.java index ac43f22b6..7607df2e0 100644 --- a/src/test/java/com/hivemq/configuration/reader/ConfigFileReaderTest.java +++ b/src/test/java/com/hivemq/configuration/reader/ConfigFileReaderTest.java @@ -29,6 +29,7 @@ import com.hivemq.mqtt.message.QoS; import com.hivemq.statistics.UsageStatisticsConfig; import com.hivemq.util.EnvVarUtil; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -63,10 +64,11 @@ public class ConfigFileReaderTest { private ListenerConfigurationService listenerConfigurationService; ConfigFileReader reader; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); listenerConfigurationService = new ListenerConfigurationServiceImpl(); final ConfigurationFile configurationFile = new ConfigurationFile(null); @@ -80,6 +82,11 @@ public void setUp() throws Exception { new PersistenceConfigurator(persistenceConfigurationService)); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void verify_mqtt_default_values() { reader.applyConfig(); diff --git a/src/test/java/com/hivemq/configuration/service/impl/SecurityConfigurationServiceImplTest.java b/src/test/java/com/hivemq/configuration/service/impl/SecurityConfigurationServiceImplTest.java index d93354eaf..087225cd8 100644 --- a/src/test/java/com/hivemq/configuration/service/impl/SecurityConfigurationServiceImplTest.java +++ b/src/test/java/com/hivemq/configuration/service/impl/SecurityConfigurationServiceImplTest.java @@ -26,7 +26,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.mockito.MockitoAnnotations.initMocks; +import static org.mockito.MockitoAnnotations.openMocks; /** * @author Waldemar Ruck @@ -38,10 +38,11 @@ public class SecurityConfigurationServiceImplTest { new SecurityConfigurationServiceImpl(); private LogbackCapturingAppender logCapture; + private AutoCloseable closeable; @Before public void setup() { - initMocks(this); + closeable = openMocks(this); final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); logCapture = LogbackCapturingAppender.Factory.weaveInto(logger); } @@ -51,6 +52,11 @@ public void tearDown() throws Exception { LogbackCapturingAppender.Factory.cleanUp(); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_defaults() { assertTrue(securityConfigurationService.allowRequestProblemInformation()); diff --git a/src/test/java/com/hivemq/extensions/ExtensionPriorityComparatorTest.java b/src/test/java/com/hivemq/extensions/ExtensionPriorityComparatorTest.java index 2e0b4668b..ac779f2a1 100644 --- a/src/test/java/com/hivemq/extensions/ExtensionPriorityComparatorTest.java +++ b/src/test/java/com/hivemq/extensions/ExtensionPriorityComparatorTest.java @@ -16,6 +16,7 @@ package com.hivemq.extensions; import com.hivemq.extension.sdk.api.annotations.NotNull; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -38,14 +39,20 @@ public class ExtensionPriorityComparatorTest { private HiveMQExtensions hiveMQExtensions; private ExtensionPriorityComparator comparator; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); comparator = new ExtensionPriorityComparator(hiveMQExtensions); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_compare_extensions() { final HiveMQExtension extension1 = getHiveMQExtension(10); diff --git a/src/test/java/com/hivemq/extensions/ListenableFutureConverterTest.java b/src/test/java/com/hivemq/extensions/ListenableFutureConverterTest.java index 0fdee0dd5..cd4db3f28 100644 --- a/src/test/java/com/hivemq/extensions/ListenableFutureConverterTest.java +++ b/src/test/java/com/hivemq/extensions/ListenableFutureConverterTest.java @@ -19,6 +19,7 @@ import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import com.hivemq.persistence.retained.RetainedMessagePersistence; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -46,9 +47,16 @@ public class ListenableFutureConverterTest { @Mock private RetainedMessagePersistence retainedMessagePersistence; + private AutoCloseable closeable; + @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); + } + + @After + public void releaseMocks() throws Exception { + closeable. close(); } @Test diff --git a/src/test/java/com/hivemq/extensions/auth/ReAuthOutputTest.java b/src/test/java/com/hivemq/extensions/auth/ReAuthOutputTest.java index 50b80e216..04b0a24a1 100644 --- a/src/test/java/com/hivemq/extensions/auth/ReAuthOutputTest.java +++ b/src/test/java/com/hivemq/extensions/auth/ReAuthOutputTest.java @@ -23,6 +23,7 @@ import com.hivemq.mqtt.message.reason.Mqtt5DisconnectReasonCode; import com.hivemq.util.Bytes; import com.hivemq.util.ReasonStrings; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -48,10 +49,11 @@ public class ReAuthOutputTest { private PluginOutPutAsyncer asyncer; private ReAuthOutput authTaskOutput; + private AutoCloseable closeable; @Before public void setUp() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); authTaskOutput = new ReAuthOutput(asyncer, true, new ModifiableDefaultPermissionsImpl(), @@ -59,6 +61,11 @@ public void setUp() { 30); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test(timeout = 5000) public void test_user_properties_of_connect_are_not_present() { final List one = authTaskOutput.getOutboundUserProperties().getAllForName("one"); diff --git a/src/test/java/com/hivemq/extensions/auth/ReAuthTaskTest.java b/src/test/java/com/hivemq/extensions/auth/ReAuthTaskTest.java index 4ed9a6a6d..7bc71689a 100644 --- a/src/test/java/com/hivemq/extensions/auth/ReAuthTaskTest.java +++ b/src/test/java/com/hivemq/extensions/auth/ReAuthTaskTest.java @@ -30,6 +30,7 @@ import com.hivemq.mqtt.message.auth.AUTH; import com.hivemq.mqtt.message.mqtt5.Mqtt5UserProperties; import com.hivemq.mqtt.message.reason.Mqtt5AuthReasonCode; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -53,27 +54,29 @@ public class ReAuthTaskTest { public static AtomicBoolean connect; - - @Mock - private WrappedAuthenticatorProvider wrappedAuthenticatorProvider; public static AtomicBoolean auth; - - private EnhancedAuthenticator enhancedAuthenticator; public static AtomicBoolean reAuth; + private EnhancedAuthenticator enhancedAuthenticator; + private IsolatedExtensionClassloader classloader; + private AutoCloseable closeable; + @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); private ReAuthTask authTask; + @Mock + private WrappedAuthenticatorProvider wrappedAuthenticatorProvider; + @Mock private AuthenticatorProviderInput authenticatorProviderInput; - private IsolatedExtensionClassloader classloader; + @Mock private HiveMQExtensions extensions; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); connect = new AtomicBoolean(); auth = new AtomicBoolean(); @@ -91,6 +94,11 @@ public void setUp() throws Exception { new ClientAuthenticatorsImpl(new ExtensionPriorityComparator(extensions))); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test(timeout = 5000) public void test_authenticator_is_same() { diff --git a/src/test/java/com/hivemq/extensions/auth/parameter/PublishAuthorizerOutputImplTest.java b/src/test/java/com/hivemq/extensions/auth/parameter/PublishAuthorizerOutputImplTest.java index 0a01a0e45..89ae95037 100644 --- a/src/test/java/com/hivemq/extensions/auth/parameter/PublishAuthorizerOutputImplTest.java +++ b/src/test/java/com/hivemq/extensions/auth/parameter/PublishAuthorizerOutputImplTest.java @@ -18,6 +18,7 @@ import com.hivemq.extension.sdk.api.packets.disconnect.DisconnectReasonCode; import com.hivemq.extension.sdk.api.packets.publish.AckReasonCode; import com.hivemq.extensions.executor.PluginOutPutAsyncer; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -40,13 +41,19 @@ public class PublishAuthorizerOutputImplTest { private PluginOutPutAsyncer asyncer; private PublishAuthorizerOutputImpl output; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); output = new PublishAuthorizerOutputImpl(asyncer); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_output_success() { output.authorizeSuccessfully(); diff --git a/src/test/java/com/hivemq/extensions/auth/parameter/SubscriptionAuthorizerInputImplTest.java b/src/test/java/com/hivemq/extensions/auth/parameter/SubscriptionAuthorizerInputImplTest.java index 455183099..5cdd33504 100644 --- a/src/test/java/com/hivemq/extensions/auth/parameter/SubscriptionAuthorizerInputImplTest.java +++ b/src/test/java/com/hivemq/extensions/auth/parameter/SubscriptionAuthorizerInputImplTest.java @@ -27,6 +27,7 @@ import com.hivemq.mqtt.message.subscribe.Topic; import io.netty.channel.Channel; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -45,17 +46,23 @@ public class SubscriptionAuthorizerInputImplTest { private Channel channel; private ClientConnection clientConnection; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); channel = new EmbeddedChannel(); clientConnection = new DummyClientConnection(channel, null); channel.attr(ClientConnectionContext.CHANNEL_ATTRIBUTE_NAME).set(clientConnection); clientConnection.setProtocolVersion(ProtocolVersion.MQTTv5); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_full_subscription() { final UserPropertiesImpl userProperties = UserPropertiesImpl.of(ImmutableList.of()); diff --git a/src/test/java/com/hivemq/extensions/auth/parameter/SubscriptionAuthorizerOutputImplTest.java b/src/test/java/com/hivemq/extensions/auth/parameter/SubscriptionAuthorizerOutputImplTest.java index 1460255ac..f23200989 100644 --- a/src/test/java/com/hivemq/extensions/auth/parameter/SubscriptionAuthorizerOutputImplTest.java +++ b/src/test/java/com/hivemq/extensions/auth/parameter/SubscriptionAuthorizerOutputImplTest.java @@ -18,6 +18,7 @@ import com.hivemq.extension.sdk.api.packets.disconnect.DisconnectReasonCode; import com.hivemq.extension.sdk.api.packets.subscribe.SubackReasonCode; import com.hivemq.extensions.executor.PluginOutPutAsyncer; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -40,13 +41,19 @@ public class SubscriptionAuthorizerOutputImplTest { private PluginOutPutAsyncer asyncer; private SubscriptionAuthorizerOutputImpl output; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); output = new SubscriptionAuthorizerOutputImpl(asyncer); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_output_success() { output.authorizeSuccessfully(); diff --git a/src/test/java/com/hivemq/extensions/executor/PluginOutputAsyncerImplTest.java b/src/test/java/com/hivemq/extensions/executor/PluginOutputAsyncerImplTest.java index f9fabe03c..09b395139 100644 --- a/src/test/java/com/hivemq/extensions/executor/PluginOutputAsyncerImplTest.java +++ b/src/test/java/com/hivemq/extensions/executor/PluginOutputAsyncerImplTest.java @@ -22,6 +22,7 @@ import com.hivemq.extension.sdk.api.async.Async; import com.hivemq.extension.sdk.api.async.TimeoutFallback; import com.hivemq.extensions.executor.task.PluginTaskOutput; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -44,6 +45,7 @@ public class PluginOutputAsyncerImplTest { private PluginOutPutAsyncer asyncer; + private AutoCloseable closeable; @Mock private ShutdownHooks shutdownHooks; @@ -51,11 +53,16 @@ public class PluginOutputAsyncerImplTest { @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); asyncer = new PluginOutputAsyncerImpl(shutdownHooks); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_output_resume() { diff --git a/src/test/java/com/hivemq/extensions/executor/PluginTaskExecutorServiceImplTest.java b/src/test/java/com/hivemq/extensions/executor/PluginTaskExecutorServiceImplTest.java index 6a8f04a74..ef8cc33f7 100644 --- a/src/test/java/com/hivemq/extensions/executor/PluginTaskExecutorServiceImplTest.java +++ b/src/test/java/com/hivemq/extensions/executor/PluginTaskExecutorServiceImplTest.java @@ -35,6 +35,7 @@ import com.hivemq.extensions.executor.task.PluginTaskOutput; import com.hivemq.persistence.local.xodus.bucket.BucketUtils; import org.apache.commons.lang3.RandomStringUtils; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -56,6 +57,7 @@ public class PluginTaskExecutorServiceImplTest { private PluginTaskExecutorServiceImpl executorService; + private AutoCloseable closeable; @Mock private PluginTaskExecutor executor1; @@ -68,7 +70,7 @@ public class PluginTaskExecutorServiceImplTest { @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); InternalConfigurations.EXTENSION_TASK_QUEUE_EXECUTOR_THREADS_COUNT.set(2); @@ -77,6 +79,11 @@ public void before() { mock(ShutdownHooks.class)); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_inout_executed_in_the_right_executor() { diff --git a/src/test/java/com/hivemq/extensions/executor/task/PluginTaskExecutorTest.java b/src/test/java/com/hivemq/extensions/executor/task/PluginTaskExecutorTest.java index 62b02de8f..b4d50497d 100644 --- a/src/test/java/com/hivemq/extensions/executor/task/PluginTaskExecutorTest.java +++ b/src/test/java/com/hivemq/extensions/executor/task/PluginTaskExecutorTest.java @@ -45,15 +45,15 @@ public class PluginTaskExecutorTest { private PluginTaskExecutor pluginTaskExecutor; - private List executionOrder; + private AutoCloseable closeable; @Mock IsolatedExtensionClassloader classloader; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); executionOrder = Collections.synchronizedList(new ArrayList<>()); pluginTaskExecutor = new PluginTaskExecutor(new AtomicLong(0)); @@ -65,6 +65,11 @@ public void after() { pluginTaskExecutor.stop(); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test(timeout = 5000) public void test_inout_task_is_executed() throws Exception { diff --git a/src/test/java/com/hivemq/extensions/iteration/AsyncLocalChunkIteratorTest.java b/src/test/java/com/hivemq/extensions/iteration/AsyncLocalChunkIteratorTest.java index b197dc443..f2f0fffdf 100644 --- a/src/test/java/com/hivemq/extensions/iteration/AsyncLocalChunkIteratorTest.java +++ b/src/test/java/com/hivemq/extensions/iteration/AsyncLocalChunkIteratorTest.java @@ -21,6 +21,7 @@ import com.google.common.util.concurrent.SettableFuture; import com.hivemq.extension.sdk.api.annotations.NotNull; import com.hivemq.extension.sdk.api.annotations.Nullable; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -50,16 +51,22 @@ public class AsyncLocalChunkIteratorTest { private TestFetchCallback fetchCallback; private TestItemCallback itemCallback; private ExecutorService executorService; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); executorService = Executors.newFixedThreadPool(2); fetchCallback = new TestFetchCallback(executorService); itemCallback = new TestItemCallback(); asyncIterator = new AsyncLocalChunkIterator<>(fetchCallback, itemCallback, executorService); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test(timeout = 15_000, expected = RuntimeException.class) public void test_fetch_failed() throws Throwable { diff --git a/src/test/java/com/hivemq/extensions/services/PluginServiceRateLimitServiceTest.java b/src/test/java/com/hivemq/extensions/services/PluginServiceRateLimitServiceTest.java index c994e3f34..7138715ac 100644 --- a/src/test/java/com/hivemq/extensions/services/PluginServiceRateLimitServiceTest.java +++ b/src/test/java/com/hivemq/extensions/services/PluginServiceRateLimitServiceTest.java @@ -17,6 +17,7 @@ import com.hivemq.configuration.service.InternalConfigurations; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -30,13 +31,19 @@ public class PluginServiceRateLimitServiceTest { private PluginServiceRateLimitService pluginServiceRateLimitService; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); pluginServiceRateLimitService = new PluginServiceRateLimitService(); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_no_limit() { assertFalse(pluginServiceRateLimitService.rateLimitExceeded()); @@ -72,4 +79,4 @@ public void test_limit_not_exceeded() { assertFalse(pluginServiceRateLimitService.rateLimitExceeded()); } -} \ No newline at end of file +} diff --git a/src/test/java/com/hivemq/extensions/services/builder/TopicSubscriptionBuilderImplTest.java b/src/test/java/com/hivemq/extensions/services/builder/TopicSubscriptionBuilderImplTest.java index 4adeebe3c..2f0d2a057 100644 --- a/src/test/java/com/hivemq/extensions/services/builder/TopicSubscriptionBuilderImplTest.java +++ b/src/test/java/com/hivemq/extensions/services/builder/TopicSubscriptionBuilderImplTest.java @@ -28,6 +28,7 @@ import com.hivemq.mqtt.message.mqtt5.Mqtt5RetainHandling; import com.hivemq.mqtt.message.subscribe.Topic; import org.apache.commons.lang3.RandomStringUtils; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -41,18 +42,23 @@ public class TopicSubscriptionBuilderImplTest { private FullConfigurationService fullConfigurationService; - private TopicSubscriptionBuilder topicSubscriptionBuilder; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); fullConfigurationService = new TestConfigurationBootstrap().getFullConfigurationService(); topicSubscriptionBuilder = new TopicSubscriptionBuilderImpl(fullConfigurationService); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_from_subscription() { diff --git a/src/test/java/com/hivemq/extensions/services/publish/PublishServiceImplTest.java b/src/test/java/com/hivemq/extensions/services/publish/PublishServiceImplTest.java index 3124ecabd..bf75c1f25 100644 --- a/src/test/java/com/hivemq/extensions/services/publish/PublishServiceImplTest.java +++ b/src/test/java/com/hivemq/extensions/services/publish/PublishServiceImplTest.java @@ -38,6 +38,7 @@ import com.hivemq.mqtt.topic.SubscriberWithIdentifiers; import com.hivemq.mqtt.topic.SubscriptionFlag; import com.hivemq.mqtt.topic.tree.LocalTopicTree; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -84,10 +85,11 @@ public class PublishServiceImplTest { private final FullConfigurationService fullConfigurationService = new TestConfigurationBootstrap().getFullConfigurationService(); private PublishServiceImpl publishService; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(rateLimitService.rateLimitExceeded()).thenReturn(false); managedPluginExecutorService = new GlobalManagedExtensionExecutorService(shutdownHooks); managedPluginExecutorService.postConstruct(); @@ -99,6 +101,11 @@ public void setUp() throws Exception { topicTree); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test(expected = DoNotImplementException.class) public void test_publish_implemented_publish() throws Throwable { try { diff --git a/src/test/java/com/hivemq/extensions/services/session/ClientServiceImplTest.java b/src/test/java/com/hivemq/extensions/services/session/ClientServiceImplTest.java index 4b5a98562..78c67e682 100644 --- a/src/test/java/com/hivemq/extensions/services/session/ClientServiceImplTest.java +++ b/src/test/java/com/hivemq/extensions/services/session/ClientServiceImplTest.java @@ -42,6 +42,7 @@ import com.hivemq.persistence.clientsession.ClientSessionPersistence; import com.hivemq.persistence.clientsession.ClientSessionWill; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -89,10 +90,11 @@ public class ClientServiceImplTest { private final String clientId = "clientID"; private final long sessionExpiry = 123546L; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); clientService = new ClientServiceImpl(pluginServiceRateLimitService, clientSessionPersistence, getManagedExtensionExecutorService(), @@ -100,6 +102,11 @@ public void setUp() throws Exception { when(pluginServiceRateLimitService.rateLimitExceeded()).thenReturn(false); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test(expected = RateLimitExceededException.class) public void test_get_session_limit_exceeded() throws Throwable { when(pluginServiceRateLimitService.rateLimitExceeded()).thenReturn(true); diff --git a/src/test/java/com/hivemq/extensions/services/subscription/SubscriptionStoreImplTest.java b/src/test/java/com/hivemq/extensions/services/subscription/SubscriptionStoreImplTest.java index a17b57f04..80b0d10c3 100644 --- a/src/test/java/com/hivemq/extensions/services/subscription/SubscriptionStoreImplTest.java +++ b/src/test/java/com/hivemq/extensions/services/subscription/SubscriptionStoreImplTest.java @@ -47,6 +47,7 @@ import com.hivemq.mqtt.topic.tree.LocalTopicTree; import com.hivemq.persistence.clientsession.ClientSessionSubscriptionPersistence; import com.hivemq.persistence.clientsession.callback.SubscriptionResult; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -86,6 +87,7 @@ public class SubscriptionStoreImplTest { private SubscriptionStore subscriptionStore; + private AutoCloseable closeable; @Mock private ClientSessionSubscriptionPersistence clientSessionSubscriptionPersistence; @@ -102,7 +104,7 @@ public class SubscriptionStoreImplTest { @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); subscriptionStore = new SubscriptionStoreImpl(clientSessionSubscriptionPersistence, rateLimitService, topicTree, @@ -111,6 +113,11 @@ public void setUp() throws Exception { when(rateLimitService.rateLimitExceeded()).thenReturn(false); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test(timeout = 10000) public void test_get_rate_limit_exceeded() { diff --git a/src/test/java/com/hivemq/limitation/TopicAliasLimiterImplTest.java b/src/test/java/com/hivemq/limitation/TopicAliasLimiterImplTest.java index 6c14e6442..079efaa49 100644 --- a/src/test/java/com/hivemq/limitation/TopicAliasLimiterImplTest.java +++ b/src/test/java/com/hivemq/limitation/TopicAliasLimiterImplTest.java @@ -18,6 +18,7 @@ import com.hivemq.configuration.service.InternalConfigurations; import org.apache.commons.lang3.RandomStringUtils; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -31,10 +32,11 @@ public class TopicAliasLimiterImplTest { private TopicAliasLimiter topicAliasLimiter; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); InternalConfigurations.TOPIC_ALIAS_GLOBAL_MEMORY_SOFT_LIMIT_BYTES.set(50); InternalConfigurations.TOPIC_ALIAS_GLOBAL_MEMORY_HARD_LIMIT_BYTES.set(200); @@ -42,6 +44,11 @@ public void setUp() throws Exception { topicAliasLimiter = new TopicAliasLimiterImpl(); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_init_usage() { @@ -120,4 +127,4 @@ public void test_finish_usage() { assertEquals(true, topicAliasLimiter.aliasesAvailable()); } -} \ No newline at end of file +} diff --git a/src/test/java/com/hivemq/metrics/gauges/OpenConnectionsGaugeTest.java b/src/test/java/com/hivemq/metrics/gauges/OpenConnectionsGaugeTest.java index 318291114..e27f72788 100644 --- a/src/test/java/com/hivemq/metrics/gauges/OpenConnectionsGaugeTest.java +++ b/src/test/java/com/hivemq/metrics/gauges/OpenConnectionsGaugeTest.java @@ -16,6 +16,7 @@ package com.hivemq.metrics.gauges; import io.netty.channel.group.ChannelGroup; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -33,13 +34,19 @@ public class OpenConnectionsGaugeTest { ChannelGroup channelGroup; private OpenConnectionsGauge gauge; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); gauge = new OpenConnectionsGauge(channelGroup); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_getValue() throws Exception { @@ -49,4 +56,4 @@ public void test_getValue() throws Exception { assertEquals(243, value.intValue()); } -} \ No newline at end of file +} diff --git a/src/test/java/com/hivemq/metrics/gauges/RetainedMessagesGaugeTest.java b/src/test/java/com/hivemq/metrics/gauges/RetainedMessagesGaugeTest.java index 3cafb7947..338c322e1 100644 --- a/src/test/java/com/hivemq/metrics/gauges/RetainedMessagesGaugeTest.java +++ b/src/test/java/com/hivemq/metrics/gauges/RetainedMessagesGaugeTest.java @@ -16,6 +16,7 @@ package com.hivemq.metrics.gauges; import com.hivemq.persistence.retained.RetainedMessagePersistence; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -33,14 +34,20 @@ public class RetainedMessagesGaugeTest { RetainedMessagePersistence retainedMessagePersistence; private RetainedMessagesGauge retainedMessagesGauge; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); retainedMessagesGauge = new RetainedMessagesGauge(retainedMessagePersistence); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_getValue() throws Exception { @@ -50,4 +57,4 @@ public void test_getValue() throws Exception { assertEquals(452l, value.longValue()); } -} \ No newline at end of file +} diff --git a/src/test/java/com/hivemq/metrics/gauges/SessionsGaugeTest.java b/src/test/java/com/hivemq/metrics/gauges/SessionsGaugeTest.java index 001e6ee0e..7fd847119 100644 --- a/src/test/java/com/hivemq/metrics/gauges/SessionsGaugeTest.java +++ b/src/test/java/com/hivemq/metrics/gauges/SessionsGaugeTest.java @@ -16,6 +16,7 @@ package com.hivemq.metrics.gauges; import com.hivemq.persistence.local.ClientSessionLocalPersistence; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -33,14 +34,20 @@ public class SessionsGaugeTest { ClientSessionLocalPersistence sessionPersistence; private SessionsGauge sessionsGauge; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); sessionsGauge = new SessionsGauge(sessionPersistence); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_getValue() throws Exception { @@ -48,4 +55,4 @@ public void test_getValue() throws Exception { final Integer value = sessionsGauge.getValue(); assertEquals(2, value.intValue()); } -} \ No newline at end of file +} diff --git a/src/test/java/com/hivemq/metrics/ioc/MetricsModuleTest.java b/src/test/java/com/hivemq/metrics/ioc/MetricsModuleTest.java index 5de129aea..0567c2786 100644 --- a/src/test/java/com/hivemq/metrics/ioc/MetricsModuleTest.java +++ b/src/test/java/com/hivemq/metrics/ioc/MetricsModuleTest.java @@ -29,6 +29,7 @@ import com.hivemq.persistence.retained.RetainedMessagePersistence; import io.netty.channel.EventLoopGroup; import io.netty.channel.group.ChannelGroup; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -40,10 +41,11 @@ public class MetricsModuleTest { private Injector injector; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); injector = Guice.createInjector(new AbstractModule() { @Override @@ -66,6 +68,11 @@ protected void configure() { }); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_metrics_registry_singleton() throws Exception { @@ -74,4 +81,4 @@ public void test_metrics_registry_singleton() throws Exception { assertSame(instance, instance2); } -} \ No newline at end of file +} diff --git a/src/test/java/com/hivemq/migration/meta/MetaFileServiceTest.java b/src/test/java/com/hivemq/migration/meta/MetaFileServiceTest.java index edd727b15..66c20c92f 100644 --- a/src/test/java/com/hivemq/migration/meta/MetaFileServiceTest.java +++ b/src/test/java/com/hivemq/migration/meta/MetaFileServiceTest.java @@ -18,6 +18,7 @@ import com.hivemq.configuration.info.SystemInformation; import com.hivemq.util.LocalPersistenceFileUtil; import org.apache.commons.lang3.RandomStringUtils; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -45,15 +46,21 @@ public class MetaFileServiceTest { private SystemInformation systemInformation; private File dataFolder; + private AutoCloseable closeable; @Before public void before() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); dataFolder = temporaryFolder.newFolder(); when(systemInformation.getDataFolder()).thenReturn(dataFolder); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_read_file_data_folder_not_present() throws Exception { diff --git a/src/test/java/com/hivemq/migration/persistence/PersistenceMigratorTest.java b/src/test/java/com/hivemq/migration/persistence/PersistenceMigratorTest.java index ae67529de..9b52f88b8 100644 --- a/src/test/java/com/hivemq/migration/persistence/PersistenceMigratorTest.java +++ b/src/test/java/com/hivemq/migration/persistence/PersistenceMigratorTest.java @@ -22,6 +22,7 @@ import com.hivemq.migration.persistence.queue.ClientQueuePayloadIDMigration; import com.hivemq.migration.persistence.retained.RetainedMessagePayloadIDMigration; import com.hivemq.migration.persistence.retained.RetainedMessageTypeMigration; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -44,15 +45,22 @@ public class PersistenceMigratorTest { @Mock private RetainedMessagePayloadIDMigration retainedMessagePayloadIDMigration; + private AutoCloseable closeable; + @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); persistenceMigrator = new PersistenceMigrator(() -> publishPayloadTypeMigration, () -> retainedMessageTypeMigration, () -> retainedMessagePayloadIDMigration, () -> clientQueuePayloadIDMigration); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_migrate_all_to_native() { diff --git a/src/test/java/com/hivemq/migration/persistence/payload/PublishPayloadTypeMigrationTest.java b/src/test/java/com/hivemq/migration/persistence/payload/PublishPayloadTypeMigrationTest.java index 6d33eae14..dfa725aca 100644 --- a/src/test/java/com/hivemq/migration/persistence/payload/PublishPayloadTypeMigrationTest.java +++ b/src/test/java/com/hivemq/migration/persistence/payload/PublishPayloadTypeMigrationTest.java @@ -68,10 +68,11 @@ public class PublishPayloadTypeMigrationTest { private File dataFolder; private FullConfigurationService configurationService; + private AutoCloseable closeable; @Before public void before() throws IOException { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); dataFolder = temporaryFolder.newFolder(); when(systemInformation.getDataFolder()).thenReturn(dataFolder); when(systemInformation.getConfigFolder()).thenReturn(temporaryFolder.newFolder()); @@ -96,6 +97,11 @@ public void tearDown() throws Exception { FileUtils.forceDelete(dataFolder); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_payload_migration_xodus_to_rocks() throws Exception { diff --git a/src/test/java/com/hivemq/migration/persistence/retained/RetainedMessageTypeMigrationTest.java b/src/test/java/com/hivemq/migration/persistence/retained/RetainedMessageTypeMigrationTest.java index ef1e48e15..93a518506 100644 --- a/src/test/java/com/hivemq/migration/persistence/retained/RetainedMessageTypeMigrationTest.java +++ b/src/test/java/com/hivemq/migration/persistence/retained/RetainedMessageTypeMigrationTest.java @@ -85,12 +85,13 @@ public class RetainedMessageTypeMigrationTest { private File dataFolder; private FullConfigurationService configurationService; + private AutoCloseable closeable; private RetainedMessageTypeMigration.RetainedMessagePersistenceTypeSwitchCallback callback; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); dataFolder = temporaryFolder.newFolder(); when(systemInformation.getDataFolder()).thenReturn(dataFolder); when(systemInformation.getConfigFolder()).thenReturn(temporaryFolder.newFolder()); @@ -121,6 +122,11 @@ public void tearDown() throws Exception { FileUtils.forceDelete(dataFolder); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test(timeout = 5000) public void test_missing_payload_gets_logged() throws Exception { diff --git a/src/test/java/com/hivemq/mqtt/InternalPublishServiceImplTest.java b/src/test/java/com/hivemq/mqtt/InternalPublishServiceImplTest.java index e3fdf10d2..ee3fe0315 100644 --- a/src/test/java/com/hivemq/mqtt/InternalPublishServiceImplTest.java +++ b/src/test/java/com/hivemq/mqtt/InternalPublishServiceImplTest.java @@ -28,6 +28,7 @@ import com.hivemq.mqtt.topic.tree.LocalTopicTree; import com.hivemq.mqtt.topic.tree.TopicSubscribers; import com.hivemq.persistence.retained.RetainedMessagePersistence; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; @@ -73,11 +74,12 @@ public class InternalPublishServiceImplTest { private ExecutorService executorService; private InternalPublishServiceImpl publishService; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); executorService = MoreExecutors.newDirectExecutorService(); @@ -91,6 +93,11 @@ public void before() { publishService = new InternalPublishServiceImpl(retainedMessagePersistence, topicTree, publishDistributor); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test(timeout = 20000) public void test_retained_message_remove() throws Exception { diff --git a/src/test/java/com/hivemq/mqtt/callback/PublishStatusFutureCallbackTest.java b/src/test/java/com/hivemq/mqtt/callback/PublishStatusFutureCallbackTest.java index 58d49623d..60fea21e3 100644 --- a/src/test/java/com/hivemq/mqtt/callback/PublishStatusFutureCallbackTest.java +++ b/src/test/java/com/hivemq/mqtt/callback/PublishStatusFutureCallbackTest.java @@ -25,6 +25,7 @@ import com.hivemq.mqtt.services.PublishPollService; import io.netty.channel.Channel; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -54,19 +55,16 @@ public class PublishStatusFutureCallbackTest { private FreePacketIdRanges messageIDPool; private boolean sharedSubscription; - private String queueId; - private PUBLISH publish; - private Channel channel; - private String client; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); sharedSubscription = false; queueId = "queueId"; publish = TestMessageUtil.createMqtt5Publish(); @@ -90,6 +88,11 @@ public void setUp() throws Exception { client); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_on_success_qos_0_new_messages_available() { diff --git a/src/test/java/com/hivemq/mqtt/handler/DropOutgoingPublishesHandlerTest.java b/src/test/java/com/hivemq/mqtt/handler/DropOutgoingPublishesHandlerTest.java index 2e578039d..b3b8c3073 100644 --- a/src/test/java/com/hivemq/mqtt/handler/DropOutgoingPublishesHandlerTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/DropOutgoingPublishesHandlerTest.java @@ -30,6 +30,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -63,10 +64,11 @@ public class DropOutgoingPublishesHandlerTest { Counter counter; private DropOutgoingPublishesHandler handler; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(ctx.channel()).thenReturn(channel); final ClientConnection clientConnection = new DummyClientConnection(channel, null); clientConnection.setClientId("clientId"); @@ -76,6 +78,11 @@ public void setUp() throws Exception { handler = new DropOutgoingPublishesHandler(messageDroppedService); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void drop_message() throws Exception { final SettableFuture future = SettableFuture.create(); diff --git a/src/test/java/com/hivemq/mqtt/handler/InterceptorHandlerTest.java b/src/test/java/com/hivemq/mqtt/handler/InterceptorHandlerTest.java index 51ae1e8bf..6e7635b9d 100644 --- a/src/test/java/com/hivemq/mqtt/handler/InterceptorHandlerTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/InterceptorHandlerTest.java @@ -44,6 +44,7 @@ import com.hivemq.mqtt.message.unsubscribe.UNSUBSCRIBE; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -89,10 +90,11 @@ public class InterceptorHandlerTest { private @NotNull ChannelPromise channelPromise; private @NotNull InterceptorHandler interceptorHandler; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); interceptorHandler = new InterceptorHandler(connectInboundInterceptorHandler, connackOutboundInterceptorHandler, publishOutboundInterceptorHandler, @@ -107,6 +109,11 @@ public void setUp() throws Exception { disconnectInterceptorHandler); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_read_connect() { interceptorHandler.channelRead(channelHandlerContext, mock(CONNECT.class)); diff --git a/src/test/java/com/hivemq/mqtt/handler/auth/AuthHandlerTest.java b/src/test/java/com/hivemq/mqtt/handler/auth/AuthHandlerTest.java index 80f5518ef..9e781fc60 100644 --- a/src/test/java/com/hivemq/mqtt/handler/auth/AuthHandlerTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/auth/AuthHandlerTest.java @@ -64,11 +64,12 @@ public class AuthHandlerTest { private AuthHandler authHandler; private EmbeddedChannel channel; private ClientConnection clientConnection; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); channel = new EmbeddedChannel(); clientConnection = new DummyClientConnection(channel, null); @@ -83,6 +84,11 @@ public void tearDown() throws Exception { verify(mqttAuthSender).logAuth(eq(channel), any(), anyBoolean()); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_read_connect_success() { diff --git a/src/test/java/com/hivemq/mqtt/handler/auth/AuthInProgressMessageHandlerTest.java b/src/test/java/com/hivemq/mqtt/handler/auth/AuthInProgressMessageHandlerTest.java index 9386bd5b9..0b5bc8b67 100644 --- a/src/test/java/com/hivemq/mqtt/handler/auth/AuthInProgressMessageHandlerTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/auth/AuthInProgressMessageHandlerTest.java @@ -31,6 +31,7 @@ import com.hivemq.mqtt.message.reason.Mqtt5ConnAckReasonCode; import com.hivemq.mqtt.message.reason.Mqtt5DisconnectReasonCode; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -53,11 +54,11 @@ public class AuthInProgressMessageHandlerTest { private MqttConnacker connacker; private EmbeddedChannel channel; + private AutoCloseable closeable; @Before public void setUp() { - MockitoAnnotations.initMocks(this); - + closeable = MockitoAnnotations.openMocks(this); connacker = new MqttConnackerImpl(eventLog); channel = new EmbeddedChannel(); @@ -67,6 +68,11 @@ public void setUp() { channel.pipeline().addFirst(new AuthInProgressMessageHandler(connacker)); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test(timeout = 5000) public void test_handler_allows_AUTH_messages() { final AUTH successAUTH = AUTH.getSuccessAUTH(); diff --git a/src/test/java/com/hivemq/mqtt/handler/connect/ConnectHandlerTest.java b/src/test/java/com/hivemq/mqtt/handler/connect/ConnectHandlerTest.java index f86f39a1c..43e85bc9f 100644 --- a/src/test/java/com/hivemq/mqtt/handler/connect/ConnectHandlerTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/connect/ConnectHandlerTest.java @@ -156,11 +156,12 @@ public class ConnectHandlerTest { private MqttServerDisconnectorImpl serverDisconnector; private @NotNull ClientConnectionContext clientConnectionContext; private ConnectionPersistence connectionPersistence; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(clientSessionPersistence.isExistent(anyString())).thenReturn(false); when(clientSessionPersistence.clientConnected(anyString(), anyBoolean(), @@ -209,6 +210,11 @@ public void tearDown() { InternalConfigurations.AUTH_DENY_UNAUTHENTICATED_CONNECTIONS.set(true); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_connect_with_session_expiry_interval_zero() { diff --git a/src/test/java/com/hivemq/mqtt/handler/connect/ConnectionLimiterHandlerTest.java b/src/test/java/com/hivemq/mqtt/handler/connect/ConnectionLimiterHandlerTest.java index a229c755e..6f54eaa1e 100644 --- a/src/test/java/com/hivemq/mqtt/handler/connect/ConnectionLimiterHandlerTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/connect/ConnectionLimiterHandlerTest.java @@ -24,6 +24,7 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPipeline; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -66,14 +67,21 @@ public class ConnectionLimiterHandlerTest { @Mock CONNECT connect; + private AutoCloseable closeable; + @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(ctx.channel()).thenReturn(channel); when(ctx.pipeline()).thenReturn(pipeline); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void exceed_maximum_connections() throws Exception { when(restrictionsEntity.maxConnections()).thenReturn(10L); diff --git a/src/test/java/com/hivemq/mqtt/handler/connect/MessageBarrierTest.java b/src/test/java/com/hivemq/mqtt/handler/connect/MessageBarrierTest.java index f988e7af0..f212617f1 100644 --- a/src/test/java/com/hivemq/mqtt/handler/connect/MessageBarrierTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/connect/MessageBarrierTest.java @@ -31,6 +31,7 @@ import io.netty.channel.ChannelDuplexHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -48,10 +49,11 @@ public class MessageBarrierTest { private EmbeddedChannel channel; private MessageBarrier messageBarrier; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); final MqttServerDisconnector mqttServerDisconnector = new MqttServerDisconnectorImpl(new EventLog()); messageBarrier = new MessageBarrier(mqttServerDisconnector); @@ -60,6 +62,11 @@ public void before() { channel.pipeline().addFirst(MQTT_MESSAGE_BARRIER, messageBarrier); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_default() { assertEquals(false, messageBarrier.getConnectReceived()); diff --git a/src/test/java/com/hivemq/mqtt/handler/connect/SubscribeMessageBarrierTest.java b/src/test/java/com/hivemq/mqtt/handler/connect/SubscribeMessageBarrierTest.java index 382f851aa..46e72b226 100644 --- a/src/test/java/com/hivemq/mqtt/handler/connect/SubscribeMessageBarrierTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/connect/SubscribeMessageBarrierTest.java @@ -24,6 +24,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -39,15 +40,21 @@ public class SubscribeMessageBarrierTest { private EmbeddedChannel channel; private SubscribeMessageBarrier subscribeMessageBarrier; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); subscribeMessageBarrier = new SubscribeMessageBarrier(); channel = new EmbeddedChannel(); channel.pipeline().addFirst(MQTT_SUBSCRIBE_MESSAGE_BARRIER, subscribeMessageBarrier); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_default() { assertEquals(false, channel.config().isAutoRead()); diff --git a/src/test/java/com/hivemq/mqtt/handler/ping/PingRequestHandlerTest.java b/src/test/java/com/hivemq/mqtt/handler/ping/PingRequestHandlerTest.java index 83f2e4388..1a363215d 100644 --- a/src/test/java/com/hivemq/mqtt/handler/ping/PingRequestHandlerTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/ping/PingRequestHandlerTest.java @@ -20,6 +20,7 @@ import com.hivemq.mqtt.message.PINGRESP; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -37,9 +38,16 @@ public class PingRequestHandlerTest { @Mock ChannelHandlerContext ctx; + private AutoCloseable closeable; + @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); + } + + @After + public void releaseMocks() throws Exception { + closeable. close(); } @Test @@ -53,4 +61,4 @@ public void test_pingreq() throws Exception { final Object o = channel.readOutbound(); assertTrue(o instanceof PINGRESP); } -} \ No newline at end of file +} diff --git a/src/test/java/com/hivemq/mqtt/handler/publish/FlowControlHandlerTest.java b/src/test/java/com/hivemq/mqtt/handler/publish/FlowControlHandlerTest.java index 3b805c3b4..a7eb36acb 100644 --- a/src/test/java/com/hivemq/mqtt/handler/publish/FlowControlHandlerTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/publish/FlowControlHandlerTest.java @@ -29,6 +29,7 @@ import com.hivemq.mqtt.message.pubrec.PUBREC; import com.hivemq.mqtt.message.reason.Mqtt5PubRecReasonCode; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -54,13 +55,15 @@ public class FlowControlHandlerTest { private MqttConfigurationServiceImpl mqttConfigurationService; + private AutoCloseable closeable; + @Mock EventLog eventLog; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); mqttConfigurationService = new MqttConfigurationServiceImpl(); mqttConfigurationService.setServerReceiveMaximum(10); @@ -71,6 +74,11 @@ public void setUp() throws Exception { } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_disconnect_after_receiving_to_many_qos_1_publishes() { diff --git a/src/test/java/com/hivemq/mqtt/handler/publish/IncomingPublishServiceTest.java b/src/test/java/com/hivemq/mqtt/handler/publish/IncomingPublishServiceTest.java index 91bec6e8d..cf1e25ccd 100644 --- a/src/test/java/com/hivemq/mqtt/handler/publish/IncomingPublishServiceTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/publish/IncomingPublishServiceTest.java @@ -43,6 +43,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -83,11 +84,12 @@ public class IncomingPublishServiceTest { private EmbeddedChannel channel; private ChannelHandlerContext ctx; private IncomingPublishService incomingPublishService; + private AutoCloseable closeable; private final ClientConnection clientConnection = new DummyClientConnection(channel, null); @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); mqttConfigurationService = Mockito.spy(new MqttConfigurationServiceImpl()); restrictionsConfigurationService = Mockito.spy(new RestrictionsConfigurationServiceImpl()); @@ -117,6 +119,11 @@ private void setupHandlerAndChannel() { ClientConnection.of(channel).setMaxPacketSizeSend(1000L); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_publishes_skipped() { ClientConnection.of(channel).setIncomingPublishesSkipRest(true); diff --git a/src/test/java/com/hivemq/mqtt/handler/publish/MessageExpiryHandlerTest.java b/src/test/java/com/hivemq/mqtt/handler/publish/MessageExpiryHandlerTest.java index 0178e0597..b46749895 100644 --- a/src/test/java/com/hivemq/mqtt/handler/publish/MessageExpiryHandlerTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/publish/MessageExpiryHandlerTest.java @@ -55,12 +55,14 @@ public class MessageExpiryHandlerTest { private ChannelHandlerContext ctx; private EmbeddedChannel channel; + private AutoCloseable closeable; LogbackCapturingAppender logCapture; + @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); final MessageExpiryHandler messageExpiryHandler = new MessageExpiryHandler(); channel = new EmbeddedChannel(); final ClientConnection clientConnection = new DummyClientConnection(channel, null); @@ -77,6 +79,11 @@ public void tearDown() throws Exception { LogbackCapturingAppender.Factory.cleanUp(); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_message_expired_qos_0() throws Exception { diff --git a/src/test/java/com/hivemq/mqtt/handler/publish/PublishFlowHandlerTest.java b/src/test/java/com/hivemq/mqtt/handler/publish/PublishFlowHandlerTest.java index 5ca02b0ec..23801e1d1 100644 --- a/src/test/java/com/hivemq/mqtt/handler/publish/PublishFlowHandlerTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/publish/PublishFlowHandlerTest.java @@ -87,10 +87,11 @@ public class PublishFlowHandlerTest { private OrderedTopicService orderedTopicService; private EmbeddedChannel channel; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); InternalConfigurations.MAX_INFLIGHT_WINDOW_SIZE_MESSAGES = 5; when(freePacketIdRanges.takeNextId()).thenReturn(100); orderedTopicService = new OrderedTopicService(); @@ -110,6 +111,11 @@ public void tearDown() throws Exception { InternalConfigurations.MAX_INFLIGHT_WINDOW_SIZE_MESSAGES = 50; } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_return_qos_1_message_id() throws Exception { diff --git a/src/test/java/com/hivemq/mqtt/handler/publish/PublishFlushHandlerTest.java b/src/test/java/com/hivemq/mqtt/handler/publish/PublishFlushHandlerTest.java index ac1a81b83..7c82ba2ab 100644 --- a/src/test/java/com/hivemq/mqtt/handler/publish/PublishFlushHandlerTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/publish/PublishFlushHandlerTest.java @@ -29,6 +29,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.EventLoop; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -45,7 +46,7 @@ import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.mockito.MockitoAnnotations.initMocks; +import static org.mockito.MockitoAnnotations.openMocks; /** * @author Daniel Krüger @@ -66,9 +67,11 @@ public class PublishFlushHandlerTest { private @NotNull PublishFlushHandler publishFlushHandler = new PublishFlushHandler(metricsHolder); + private AutoCloseable closeable; + @Before public void setUp() { - initMocks(this); + closeable = openMocks(this); when(channelHandlerContext.channel()).thenReturn(channel); when(channel.eventLoop()).thenReturn(eventLoop); @@ -79,6 +82,11 @@ public void setUp() { when(channelHandlerContext.write(any())).thenReturn(mock(ChannelFuture.class)); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void whenPublishesAreAdded_thenConsumptionIsTriggered() { embeddedChannel.pipeline().addLast(publishFlushHandler); diff --git a/src/test/java/com/hivemq/mqtt/handler/publish/PublishWriteFailedListenerTest.java b/src/test/java/com/hivemq/mqtt/handler/publish/PublishWriteFailedListenerTest.java index a41da1eee..07b01f2c8 100644 --- a/src/test/java/com/hivemq/mqtt/handler/publish/PublishWriteFailedListenerTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/publish/PublishWriteFailedListenerTest.java @@ -18,6 +18,7 @@ import com.google.common.util.concurrent.SettableFuture; import io.netty.channel.ChannelFuture; import io.netty.handler.codec.EncoderException; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -36,9 +37,16 @@ public class PublishWriteFailedListenerTest { @Mock ChannelFuture channelFuture; + private AutoCloseable closeable; + @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); + } + + @After + public void releaseMocks() throws Exception { + closeable. close(); } @Test @@ -64,4 +72,4 @@ public void test_channel_closed() throws Exception { assertEquals(PublishStatus.NOT_CONNECTED, statusFuture.get()); } -} \ No newline at end of file +} diff --git a/src/test/java/com/hivemq/mqtt/handler/subscribe/retained/SendRetainedMessageListenerAndScheduleNextTest.java b/src/test/java/com/hivemq/mqtt/handler/subscribe/retained/SendRetainedMessageListenerAndScheduleNextTest.java index 981e1a2d8..ad402d4aa 100644 --- a/src/test/java/com/hivemq/mqtt/handler/subscribe/retained/SendRetainedMessageListenerAndScheduleNextTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/subscribe/retained/SendRetainedMessageListenerAndScheduleNextTest.java @@ -23,6 +23,7 @@ import com.hivemq.mqtt.message.subscribe.Topic; import io.netty.channel.Channel; import io.netty.channel.DefaultEventLoop; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -53,16 +54,22 @@ public class SendRetainedMessageListenerAndScheduleNextTest { private Channel channel; private ClientConnection clientConnection; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); clientConnection = new DummyClientConnection(channel, null); when(channel.attr(ClientConnectionContext.CHANNEL_ATTRIBUTE_NAME)).thenReturn(new TestChannelAttribute<>( clientConnection)); when(channel.eventLoop()).thenReturn(new DefaultEventLoop(Executors.newSingleThreadExecutor())); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void success() { when(channel.isActive()).thenReturn(true); diff --git a/src/test/java/com/hivemq/mqtt/handler/subscribe/retained/SendRetainedMessagesListenerTest.java b/src/test/java/com/hivemq/mqtt/handler/subscribe/retained/SendRetainedMessagesListenerTest.java index 0a2663f8b..28843584a 100644 --- a/src/test/java/com/hivemq/mqtt/handler/subscribe/retained/SendRetainedMessagesListenerTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/subscribe/retained/SendRetainedMessagesListenerTest.java @@ -32,6 +32,7 @@ import com.hivemq.persistence.retained.RetainedMessagePersistence; import io.netty.channel.ChannelFuture; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -63,11 +64,12 @@ @SuppressWarnings("NullabilityAnnotations") public class SendRetainedMessagesListenerTest { + private Set ignoredTopics; + private AutoCloseable closeable; + @Mock private RetainedMessagePersistence retainedMessagePersistence; - private Set ignoredTopics; - @Mock private ChannelFuture channelFuture; @@ -79,10 +81,15 @@ public class SendRetainedMessagesListenerTest { @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); ignoredTopics = new LinkedHashSet<>(); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_no_retained_message_available() throws Exception { diff --git a/src/test/java/com/hivemq/mqtt/handler/unsubscribe/UnsubscribeHandlerTest.java b/src/test/java/com/hivemq/mqtt/handler/unsubscribe/UnsubscribeHandlerTest.java index bf072a943..190ad3e6e 100644 --- a/src/test/java/com/hivemq/mqtt/handler/unsubscribe/UnsubscribeHandlerTest.java +++ b/src/test/java/com/hivemq/mqtt/handler/unsubscribe/UnsubscribeHandlerTest.java @@ -29,6 +29,7 @@ import com.hivemq.persistence.clientsession.ClientSessionSubscriptionPersistence; import com.hivemq.persistence.clientsession.SharedSubscriptionService; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -59,10 +60,11 @@ public class UnsubscribeHandlerTest { private @NotNull UnsubscribeHandler unsubscribeHandler; private @NotNull EmbeddedChannel channel; private @NotNull ClientConnection clientConnection; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); unsubscribeHandler = new UnsubscribeHandler(clientSessionSubscriptionPersistence, sharedSubscriptionService); clientConnection = new DummyClientConnection(channel, null); channel = new EmbeddedChannel(unsubscribeHandler); @@ -75,6 +77,11 @@ public void setUp() throws Exception { Futures.immediateFuture(null)); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void writeInbound_forASingleTopicAndTheDefaultProtocolVersion_removesTheSubscriptionFromThePersistenceAndAcknowledgesWithSuccess() { final String topic = "myTopic"; diff --git a/src/test/java/com/hivemq/mqtt/ioc/MQTTHandlerModuleTest.java b/src/test/java/com/hivemq/mqtt/ioc/MQTTHandlerModuleTest.java index 74215d08a..7ea7ec338 100644 --- a/src/test/java/com/hivemq/mqtt/ioc/MQTTHandlerModuleTest.java +++ b/src/test/java/com/hivemq/mqtt/ioc/MQTTHandlerModuleTest.java @@ -22,6 +22,7 @@ import com.hivemq.bootstrap.ioc.lazysingleton.LazySingletonScope; import com.hivemq.mqtt.message.dropping.MessageDroppedService; import com.hivemq.mqtt.message.dropping.MessageDroppedServiceImpl; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -37,10 +38,11 @@ public class MQTTHandlerModuleTest { private Injector injector; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { @@ -53,6 +55,11 @@ protected void configure() { }); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_message_dropped_service_is_singleton() { final MessageDroppedService instance1 = injector.getInstance(MessageDroppedService.class); diff --git a/src/test/java/com/hivemq/mqtt/message/dropping/MessageDroppedServiceImplTest.java b/src/test/java/com/hivemq/mqtt/message/dropping/MessageDroppedServiceImplTest.java index 4813e7abd..ca9d38fa9 100644 --- a/src/test/java/com/hivemq/mqtt/message/dropping/MessageDroppedServiceImplTest.java +++ b/src/test/java/com/hivemq/mqtt/message/dropping/MessageDroppedServiceImplTest.java @@ -18,6 +18,7 @@ import com.codahale.metrics.MetricRegistry; import com.hivemq.logging.EventLog; import com.hivemq.metrics.MetricsHolder; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -39,16 +40,22 @@ public class MessageDroppedServiceImplTest { private EventLog eventLog; private MessageDroppedService messageDroppedService; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); messageDroppedService = new MessageDroppedServiceImpl(new MetricsHolder(new MetricRegistry()), eventLog); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_queue_full_is_responsible() { messageDroppedService.queueFull(clientId, topic, qos); diff --git a/src/test/java/com/hivemq/mqtt/topic/tree/TestRemoveSubscriberFromTopicInTopicTreeImpl.java b/src/test/java/com/hivemq/mqtt/topic/tree/TestRemoveSubscriberFromTopicInTopicTreeImpl.java index 282cd90fd..b36843f70 100644 --- a/src/test/java/com/hivemq/mqtt/topic/tree/TestRemoveSubscriberFromTopicInTopicTreeImpl.java +++ b/src/test/java/com/hivemq/mqtt/topic/tree/TestRemoveSubscriberFromTopicInTopicTreeImpl.java @@ -20,6 +20,7 @@ import com.hivemq.mqtt.message.QoS; import com.hivemq.mqtt.message.subscribe.Topic; import com.hivemq.mqtt.topic.SubscriberWithQoS; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -34,15 +35,21 @@ public class TestRemoveSubscriberFromTopicInTopicTreeImpl { private LocalTopicTree topicTree; + private AutoCloseable closeable; @Before public void setUp() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); TOPIC_TREE_MAP_CREATION_THRESHOLD.set(1); topicTree = new LocalTopicTree(new MetricsHolder(new MetricRegistry())); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_remove_from_empty_tree() { topicTree.removeSubscriber("subscriber", "topic", null); diff --git a/src/test/java/com/hivemq/mqtt/topic/tree/TopicTreeImplExistingSubscriptionTest.java b/src/test/java/com/hivemq/mqtt/topic/tree/TopicTreeImplExistingSubscriptionTest.java index ffe93f8ab..3160b28bf 100644 --- a/src/test/java/com/hivemq/mqtt/topic/tree/TopicTreeImplExistingSubscriptionTest.java +++ b/src/test/java/com/hivemq/mqtt/topic/tree/TopicTreeImplExistingSubscriptionTest.java @@ -20,6 +20,7 @@ import com.hivemq.metrics.MetricsHolder; import com.hivemq.mqtt.message.QoS; import com.hivemq.mqtt.message.subscribe.Topic; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -33,15 +34,21 @@ public class TopicTreeImplExistingSubscriptionTest { private LocalTopicTree topicTree; + private AutoCloseable closeable; @Before public void setUp() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); InternalConfigurations.TOPIC_TREE_MAP_CREATION_THRESHOLD.set(1); topicTree = new LocalTopicTree(new MetricsHolder(new MetricRegistry())); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_existing_root_wildcard() { diff --git a/src/test/java/com/hivemq/mqtt/topic/tree/TopicTreeStartupTest.java b/src/test/java/com/hivemq/mqtt/topic/tree/TopicTreeStartupTest.java index 3b5c82528..ab2617413 100644 --- a/src/test/java/com/hivemq/mqtt/topic/tree/TopicTreeStartupTest.java +++ b/src/test/java/com/hivemq/mqtt/topic/tree/TopicTreeStartupTest.java @@ -31,6 +31,7 @@ import com.hivemq.persistence.clientsession.ClientSessionPersistence; import com.hivemq.persistence.clientsession.ClientSessionSubscriptionPersistence; import com.hivemq.persistence.clientsession.SharedSubscriptionService; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -62,10 +63,11 @@ public class TopicTreeStartupTest { private LocalTopicTree topicTree; private TopicTreeStartup topicTreeStartup; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); topicTree = new LocalTopicTree(new MetricsHolder(new MetricRegistry())); @@ -75,6 +77,11 @@ public void setUp() throws Exception { sharedSubscriptionService); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_populate_topic_tree() throws Exception { diff --git a/src/test/java/com/hivemq/persistence/InMemoryProducerQueuesTest.java b/src/test/java/com/hivemq/persistence/InMemoryProducerQueuesTest.java index 9ab098766..22e50436e 100644 --- a/src/test/java/com/hivemq/persistence/InMemoryProducerQueuesTest.java +++ b/src/test/java/com/hivemq/persistence/InMemoryProducerQueuesTest.java @@ -17,6 +17,7 @@ import com.google.common.collect.ImmutableList; import com.hivemq.extension.sdk.api.annotations.NotNull; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -36,12 +37,19 @@ public class InMemoryProducerQueuesTest { @NotNull private InMemoryProducerQueues producerQueues; + private AutoCloseable closeable; + @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); producerQueues = new InMemoryProducerQueues(64, 4); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_create_bucket_indexes() { final ImmutableList indexes0 = producerQueues.createBucketIndexes(0, 3); diff --git a/src/test/java/com/hivemq/persistence/PersistenceShutdownHookTest.java b/src/test/java/com/hivemq/persistence/PersistenceShutdownHookTest.java index 68dcee5dc..e8202b3d1 100644 --- a/src/test/java/com/hivemq/persistence/PersistenceShutdownHookTest.java +++ b/src/test/java/com/hivemq/persistence/PersistenceShutdownHookTest.java @@ -26,6 +26,7 @@ import com.hivemq.persistence.payload.PublishPayloadPersistence; import com.hivemq.persistence.qos.IncomingMessageFlowPersistence; import com.hivemq.persistence.retained.RetainedMessagePersistence; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -59,10 +60,11 @@ public class PersistenceShutdownHookTest { private ClientQueuePersistence clientQueuePersistence; private PersistenceShutdownHook persistenceShutdownHook; + private AutoCloseable closeable; @Before public void init() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); persistenceShutdownHook = new PersistenceShutdownHook(clientSessionPersistence, clientSessionSubscriptionPersistence, incomingMessageFlowPersistence, @@ -80,6 +82,11 @@ public void init() { when(clientQueuePersistence.closeDB()).thenReturn(Futures.immediateFuture(null)); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_dbs_closed() throws Exception { persistenceShutdownHook.run(); diff --git a/src/test/java/com/hivemq/persistence/ProducerQueuesImplTest.java b/src/test/java/com/hivemq/persistence/ProducerQueuesImplTest.java index 2e2e67bac..9ab65565f 100644 --- a/src/test/java/com/hivemq/persistence/ProducerQueuesImplTest.java +++ b/src/test/java/com/hivemq/persistence/ProducerQueuesImplTest.java @@ -16,6 +16,7 @@ package com.hivemq.persistence; import com.hivemq.extension.sdk.api.annotations.NotNull; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -39,9 +40,11 @@ public class ProducerQueuesImplTest { @NotNull ProducerQueuesImpl producerQueues; + private AutoCloseable closeable; + @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(singleWriterServiceImpl.getPersistenceBucketCount()).thenReturn(64); when(singleWriterServiceImpl.getThreadPoolSize()).thenReturn(4); @@ -50,6 +53,11 @@ public void setUp() throws Exception { producerQueues = new ProducerQueuesImpl(singleWriterServiceImpl, 4); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void submit_task() throws Exception { producerQueues.submit("key", bucketIndex -> null); diff --git a/src/test/java/com/hivemq/persistence/ScheduledCleanUpServiceTest.java b/src/test/java/com/hivemq/persistence/ScheduledCleanUpServiceTest.java index ac5a9f63b..dd2372a4c 100644 --- a/src/test/java/com/hivemq/persistence/ScheduledCleanUpServiceTest.java +++ b/src/test/java/com/hivemq/persistence/ScheduledCleanUpServiceTest.java @@ -25,6 +25,7 @@ import com.hivemq.persistence.clientsession.ClientSessionSubscriptionPersistence; import com.hivemq.persistence.retained.RetainedMessagePersistence; import org.awaitility.Awaitility; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; @@ -71,10 +72,11 @@ public class ScheduledCleanUpServiceTest { private ClientQueuePersistence clientQueuePersistence; private ScheduledCleanUpService scheduledCleanUpService; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); scheduledCleanUpService = new ScheduledCleanUpService(scheduledExecutorService, clientSessionPersistence, subscriptionPersistence, @@ -84,6 +86,11 @@ public void setUp() throws Exception { InternalConfigurations.PERSISTENCE_BUCKET_COUNT.set(64); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void cleanUp_invokesRespectivePersistenceCleanUps() { scheduledCleanUpService.cleanUp(0, ScheduledCleanUpService.CLIENT_SESSION_PERSISTENCE_INDEX); diff --git a/src/test/java/com/hivemq/persistence/clientqueue/ClientQueuePersistenceSerializerTest.java b/src/test/java/com/hivemq/persistence/clientqueue/ClientQueuePersistenceSerializerTest.java index 7ae256bd7..7dcbc2a64 100644 --- a/src/test/java/com/hivemq/persistence/clientqueue/ClientQueuePersistenceSerializerTest.java +++ b/src/test/java/com/hivemq/persistence/clientqueue/ClientQueuePersistenceSerializerTest.java @@ -26,6 +26,7 @@ import com.hivemq.mqtt.message.publish.PUBLISHFactory; import com.hivemq.mqtt.message.pubrel.PUBREL; import jetbrains.exodus.ByteIterable; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -44,14 +45,20 @@ public class ClientQueuePersistenceSerializerTest { private ClientQueuePersistenceSerializer serializer; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); serializer = new ClientQueuePersistenceSerializer(); ClientQueuePersistenceSerializer.NEXT_PUBLISH_NUMBER.set(Long.MAX_VALUE / 2); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_deserialize_index() { final Key key = new Key("client", false); diff --git a/src/test/java/com/hivemq/persistence/clientsession/task/ClientSessionCleanUpTaskTest.java b/src/test/java/com/hivemq/persistence/clientsession/task/ClientSessionCleanUpTaskTest.java index 9a69194da..c62761b44 100644 --- a/src/test/java/com/hivemq/persistence/clientsession/task/ClientSessionCleanUpTaskTest.java +++ b/src/test/java/com/hivemq/persistence/clientsession/task/ClientSessionCleanUpTaskTest.java @@ -19,6 +19,7 @@ import com.hivemq.persistence.clientsession.ClientSessionPersistenceImpl; import com.hivemq.persistence.clientsession.PendingWillMessages; import com.hivemq.persistence.local.ClientSessionLocalPersistence; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -42,13 +43,19 @@ public class ClientSessionCleanUpTaskTest { private PendingWillMessages pendingWillMessages; private ClientSessionCleanUpTask task; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); task = new ClientSessionCleanUpTask(localPersistence, clientSessionPersistence, pendingWillMessages); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_clean_up_clean_task() { final String clientId = "client"; @@ -57,4 +64,4 @@ public void test_clean_up_clean_task() { verify(pendingWillMessages).sendWillIfPending(clientId); verify(clientSessionPersistence).cleanClientData(clientId); } -} \ No newline at end of file +} diff --git a/src/test/java/com/hivemq/persistence/ioc/LocalPersistenceModuleTest.java b/src/test/java/com/hivemq/persistence/ioc/LocalPersistenceModuleTest.java index 0b193d59f..fbcca758a 100644 --- a/src/test/java/com/hivemq/persistence/ioc/LocalPersistenceModuleTest.java +++ b/src/test/java/com/hivemq/persistence/ioc/LocalPersistenceModuleTest.java @@ -55,6 +55,7 @@ import com.hivemq.persistence.payload.PublishPayloadXodusLocalPersistence; import com.hivemq.persistence.retained.RetainedMessageLocalPersistence; import com.hivemq.throttling.ioc.ThrottlingModule; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -110,9 +111,11 @@ public class LocalPersistenceModuleTest { @Mock private Injector persistenceInjector; + private AutoCloseable closeable; + @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(metricsHolder.getMetricRegistry()).thenReturn(new MetricRegistry()); when(persistenceInjector.getInstance(PublishPayloadPersistence.class)).thenReturn(Mockito.mock( @@ -145,6 +148,11 @@ public void setUp() throws Exception { } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_singletons() throws Exception { diff --git a/src/test/java/com/hivemq/persistence/ioc/PersistenceMigrationModuleTest.java b/src/test/java/com/hivemq/persistence/ioc/PersistenceMigrationModuleTest.java index 19f329345..b9a1c9ce3 100644 --- a/src/test/java/com/hivemq/persistence/ioc/PersistenceMigrationModuleTest.java +++ b/src/test/java/com/hivemq/persistence/ioc/PersistenceMigrationModuleTest.java @@ -29,6 +29,7 @@ import com.hivemq.persistence.payload.PublishPayloadNoopPersistenceImpl; import com.hivemq.persistence.payload.PublishPayloadPersistence; import com.hivemq.persistence.retained.RetainedMessageLocalPersistence; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -53,12 +54,19 @@ public class PersistenceMigrationModuleTest { @Mock private PersistenceConfigurationService persistenceConfigurationService; + private AutoCloseable closeable; + @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(persistenceConfigurationService.getMode()).thenReturn(PersistenceConfigurationService.PersistenceMode.FILE); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_startup_singleton() { final Injector injector = Guice.createInjector(new PersistenceMigrationModule(new MetricRegistry(), diff --git a/src/test/java/com/hivemq/persistence/ioc/PersistenceModuleTest.java b/src/test/java/com/hivemq/persistence/ioc/PersistenceModuleTest.java index 2852b932d..d4b19f674 100644 --- a/src/test/java/com/hivemq/persistence/ioc/PersistenceModuleTest.java +++ b/src/test/java/com/hivemq/persistence/ioc/PersistenceModuleTest.java @@ -42,6 +42,7 @@ import com.hivemq.persistence.payload.PublishPayloadPersistence; import com.hivemq.persistence.payload.PublishPayloadPersistenceImpl; import com.hivemq.persistence.payload.PublishPayloadRocksDBLocalPersistence; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -62,9 +63,11 @@ public class PersistenceModuleTest { @Mock private Injector persistenceInjector; + private AutoCloseable closeable; + @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(persistenceInjector.getInstance(PublishPayloadPersistence.class)).thenReturn(Mockito.mock( PublishPayloadPersistence.class)); @@ -85,6 +88,11 @@ public void setUp() throws Exception { when(persistenceInjector.getInstance(ShutdownHooks.class)).thenReturn(Mockito.mock(ShutdownHooks.class)); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_shutdown_singleton() throws ClassNotFoundException { diff --git a/src/test/java/com/hivemq/persistence/local/memory/ClientQueueMemoryLocalPersistenceTest.java b/src/test/java/com/hivemq/persistence/local/memory/ClientQueueMemoryLocalPersistenceTest.java index 55b47142b..c56045796 100644 --- a/src/test/java/com/hivemq/persistence/local/memory/ClientQueueMemoryLocalPersistenceTest.java +++ b/src/test/java/com/hivemq/persistence/local/memory/ClientQueueMemoryLocalPersistenceTest.java @@ -74,10 +74,11 @@ public class ClientQueueMemoryLocalPersistenceTest { private final long byteLimit = 5 * 1024 * 1024; private MetricRegistry metricRegistry; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); InternalConfigurations.PERSISTENCE_BUCKET_COUNT.set(bucketCount); InternalConfigurations.QOS_0_MEMORY_HARD_LIMIT_DIVISOR.set(10000); @@ -93,6 +94,11 @@ public void tearDown() throws Exception { InternalConfigurations.EXPIRE_INFLIGHT_PUBRELS_ENABLED = false; } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_readNew_lessAvailable() { final PUBLISH publish = createPublish(10, QoS.AT_LEAST_ONCE, "topic1"); diff --git a/src/test/java/com/hivemq/persistence/local/memory/ClientSessionSubscriptionMemoryLocalPersistenceTest.java b/src/test/java/com/hivemq/persistence/local/memory/ClientSessionSubscriptionMemoryLocalPersistenceTest.java index 7461ef016..c06550b5f 100644 --- a/src/test/java/com/hivemq/persistence/local/memory/ClientSessionSubscriptionMemoryLocalPersistenceTest.java +++ b/src/test/java/com/hivemq/persistence/local/memory/ClientSessionSubscriptionMemoryLocalPersistenceTest.java @@ -31,6 +31,7 @@ import com.hivemq.util.ObjectMemoryEstimation; import net.jodah.concurrentunit.Waiter; import org.apache.commons.lang3.RandomStringUtils; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -74,10 +75,11 @@ public class ClientSessionSubscriptionMemoryLocalPersistenceTest { private final int bucketCount = 4; private MetricRegistry metricRegistry; + private AutoCloseable closeable; @Before public void before() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); InternalConfigurations.PERSISTENCE_BUCKET_COUNT.set(bucketCount); when(localPersistenceFileUtil.getVersionedLocalPersistenceFolder(anyString(), anyString())).thenReturn( @@ -87,6 +89,11 @@ public void before() throws Exception { persistence = new ClientSessionSubscriptionMemoryLocalPersistence(metricRegistry); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_add_get_subscriptions() { diff --git a/src/test/java/com/hivemq/persistence/local/xodus/EnvironmentCloserTest.java b/src/test/java/com/hivemq/persistence/local/xodus/EnvironmentCloserTest.java index b0250560a..2370f4356 100644 --- a/src/test/java/com/hivemq/persistence/local/xodus/EnvironmentCloserTest.java +++ b/src/test/java/com/hivemq/persistence/local/xodus/EnvironmentCloserTest.java @@ -20,6 +20,7 @@ import jetbrains.exodus.env.Environment; import jetbrains.exodus.env.Environments; import jetbrains.exodus.env.Transaction; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -46,13 +47,20 @@ public class EnvironmentCloserTest { @Mock LocalPersistenceFileUtil localPersistenceFileUtil; + private AutoCloseable closeable; + @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(localPersistenceFileUtil.getLocalPersistenceFolder()).thenReturn(temporaryFolder.newFolder()); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test(timeout = 10000) public void test_xodus_closer_retries_until_transaction_finished() throws Exception { final CountDownLatch latch = new CountDownLatch(1); @@ -150,4 +158,4 @@ public void test_environment_closer_retryInterval_invalid() throws Exception { new EnvironmentCloser("name", mock(Environment.class), 1, 0); } -} \ No newline at end of file +} diff --git a/src/test/java/com/hivemq/persistence/local/xodus/XodusUtilsTest.java b/src/test/java/com/hivemq/persistence/local/xodus/XodusUtilsTest.java index 53b06df05..a36756c9d 100644 --- a/src/test/java/com/hivemq/persistence/local/xodus/XodusUtilsTest.java +++ b/src/test/java/com/hivemq/persistence/local/xodus/XodusUtilsTest.java @@ -19,6 +19,7 @@ import jetbrains.exodus.ByteIterable; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomUtils; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -37,9 +38,16 @@ */ public class XodusUtilsTest { + private AutoCloseable closeable; + @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); + } + + @After + public void releaseMocks() throws Exception { + closeable. close(); } @Test diff --git a/src/test/java/com/hivemq/persistence/local/xodus/bucket/BucketTest.java b/src/test/java/com/hivemq/persistence/local/xodus/bucket/BucketTest.java index 0736f4851..e79186dc6 100644 --- a/src/test/java/com/hivemq/persistence/local/xodus/bucket/BucketTest.java +++ b/src/test/java/com/hivemq/persistence/local/xodus/bucket/BucketTest.java @@ -17,6 +17,7 @@ import jetbrains.exodus.env.Environment; import jetbrains.exodus.env.Store; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -36,9 +37,16 @@ public class BucketTest { @Mock Store store; + private AutoCloseable closeable; + @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); + } + + @After + public void releaseMocks() throws Exception { + closeable. close(); } @Test @@ -48,4 +56,4 @@ public void close() throws Exception { assertFalse(bucket.close()); assertFalse(bucket.close()); } -} \ No newline at end of file +} diff --git a/src/test/java/com/hivemq/persistence/qos/IncomingMessageFlowPersistenceImplTest.java b/src/test/java/com/hivemq/persistence/qos/IncomingMessageFlowPersistenceImplTest.java index 3680aaea6..615c2b388 100644 --- a/src/test/java/com/hivemq/persistence/qos/IncomingMessageFlowPersistenceImplTest.java +++ b/src/test/java/com/hivemq/persistence/qos/IncomingMessageFlowPersistenceImplTest.java @@ -17,6 +17,7 @@ import com.hivemq.mqtt.message.publish.PUBLISH; import com.hivemq.persistence.local.IncomingMessageFlowLocalPersistence; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -32,16 +33,22 @@ public class IncomingMessageFlowPersistenceImplTest { private IncomingMessageFlowPersistenceImpl incomingMessageFlowPersistence; + private AutoCloseable closeable; @Mock private IncomingMessageFlowLocalPersistence incomingMessageFlowLocalPersistence; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); incomingMessageFlowPersistence = new IncomingMessageFlowPersistenceImpl(incomingMessageFlowLocalPersistence); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_delegate_get() { incomingMessageFlowPersistence.get("client", 1); @@ -72,4 +79,4 @@ public void test_delegate_closeDB() { incomingMessageFlowPersistence.closeDB(); verify(incomingMessageFlowLocalPersistence).closeDB(); } -} \ No newline at end of file +} diff --git a/src/test/java/com/hivemq/security/ssl/NonSslHandlerTest.java b/src/test/java/com/hivemq/security/ssl/NonSslHandlerTest.java index be566206c..d364f26a5 100644 --- a/src/test/java/com/hivemq/security/ssl/NonSslHandlerTest.java +++ b/src/test/java/com/hivemq/security/ssl/NonSslHandlerTest.java @@ -25,6 +25,7 @@ import com.hivemq.mqtt.handler.disconnect.MqttServerDisconnectorImpl; import io.netty.buffer.ByteBuf; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -43,6 +44,8 @@ public class NonSslHandlerTest { private @NotNull MqttServerDisconnector disconnector; + private AutoCloseable closeable; + private static final byte @NotNull [] VALID_SSL_PACKET = { 22, 3, @@ -655,7 +658,7 @@ public class NonSslHandlerTest { @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); final Listener listener = mock(TcpListener.class); channel = new EmbeddedChannel(); channel.attr(ClientConnectionContext.CHANNEL_ATTRIBUTE_NAME) @@ -664,6 +667,11 @@ public void setUp() throws Exception { channel.pipeline().addLast(new NonSslHandler(disconnector)); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_non_ssl_decode_to_small() { diff --git a/src/test/java/com/hivemq/security/ssl/SslClientCertificateHandlerTest.java b/src/test/java/com/hivemq/security/ssl/SslClientCertificateHandlerTest.java index 3c3bdb16e..bf550d803 100644 --- a/src/test/java/com/hivemq/security/ssl/SslClientCertificateHandlerTest.java +++ b/src/test/java/com/hivemq/security/ssl/SslClientCertificateHandlerTest.java @@ -30,6 +30,7 @@ import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.ssl.SslHandler; import io.netty.handler.ssl.SslHandshakeCompletionEvent; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -58,6 +59,7 @@ public class SslClientCertificateHandlerTest { private EmbeddedChannel channel; private UndefinedClientConnection clientConnection; + private AutoCloseable closeable; @Mock private MqttServerDisconnectorImpl mqttServerDisconnector; @@ -76,7 +78,7 @@ public class SslClientCertificateHandlerTest { @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(sslHandler.engine()).thenReturn(sslEngine); when(sslEngine.getSession()).thenReturn(sslSession); @@ -90,6 +92,11 @@ public void setUp() throws Exception { channel.pipeline().addLast(ChannelHandlerNames.SSL_HANDLER, sslHandler); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_user_event_not_ssl_cert_event() { diff --git a/src/test/java/com/hivemq/security/ssl/SslExceptionHandlerTest.java b/src/test/java/com/hivemq/security/ssl/SslExceptionHandlerTest.java index 0f5e98841..19cdb5fdd 100644 --- a/src/test/java/com/hivemq/security/ssl/SslExceptionHandlerTest.java +++ b/src/test/java/com/hivemq/security/ssl/SslExceptionHandlerTest.java @@ -26,6 +26,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.ssl.NotSslRecordException; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -61,9 +62,11 @@ public class SslExceptionHandlerTest { SslExceptionHandler sslExceptionHandler; + private AutoCloseable closeable; + @Before public void setUp() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(ctx.channel()).thenReturn(channel); final Listener listener = mock(TcpListener.class); @@ -79,6 +82,11 @@ public void setUp() { sslExceptionHandler = new SslExceptionHandler(mqttServerDisconnector); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_ignorable_exception() throws Exception { sslExceptionHandler.exceptionCaught(ctx, new NotSslRecordException()); diff --git a/src/test/java/com/hivemq/security/ssl/SslParameterHandlerTest.java b/src/test/java/com/hivemq/security/ssl/SslParameterHandlerTest.java index 420267fbc..39f04eb91 100644 --- a/src/test/java/com/hivemq/security/ssl/SslParameterHandlerTest.java +++ b/src/test/java/com/hivemq/security/ssl/SslParameterHandlerTest.java @@ -21,6 +21,7 @@ import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.ssl.SslHandler; import io.netty.handler.ssl.SslHandshakeCompletionEvent; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -42,6 +43,7 @@ public class SslParameterHandlerTest { private EmbeddedChannel channel; + private AutoCloseable closeable; @Mock private SslHandler sslHandler; @@ -54,7 +56,7 @@ public class SslParameterHandlerTest { @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); channel = new EmbeddedChannel(); channel.attr(ClientConnectionContext.CHANNEL_ATTRIBUTE_NAME).set(new DummyClientConnection(channel, null)); @@ -62,6 +64,11 @@ public void setUp() throws Exception { channel.pipeline().addLast(ChannelHandlerNames.SSL_HANDLER, sslHandler); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_other_user_event() { channel.pipeline().fireUserEventTriggered(""); diff --git a/src/test/java/com/hivemq/statistics/HivemqIdTest.java b/src/test/java/com/hivemq/statistics/HivemqIdTest.java index f4165bd3b..fe742bd0e 100644 --- a/src/test/java/com/hivemq/statistics/HivemqIdTest.java +++ b/src/test/java/com/hivemq/statistics/HivemqIdTest.java @@ -17,6 +17,7 @@ import com.hivemq.configuration.info.SystemInformation; import org.apache.commons.io.FileUtils; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -42,16 +43,21 @@ public class HivemqIdTest { private SystemInformation systemInformation; private HivemqId hivemqId; + private AutoCloseable closeable; @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(systemInformation.getDataFolder()).thenReturn(temporaryFolder.getRoot()); hivemqId = new HivemqId(systemInformation); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } @Test public void test_no_id_present() { @@ -82,4 +88,4 @@ public void test_read_from_file() throws Exception { assertEquals(id, hivemqId.getHivemqId()); } -} \ No newline at end of file +} diff --git a/src/test/java/com/hivemq/util/EnvVarUtilTest.java b/src/test/java/com/hivemq/util/EnvVarUtilTest.java index 8a665bbb0..a8cefd9b7 100644 --- a/src/test/java/com/hivemq/util/EnvVarUtilTest.java +++ b/src/test/java/com/hivemq/util/EnvVarUtilTest.java @@ -16,6 +16,7 @@ package com.hivemq.util; import com.hivemq.exceptions.UnrecoverableException; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -39,12 +40,19 @@ public class EnvVarUtilTest { @Mock EnvVarUtil envVarUtil; + private AutoCloseable closeable; + @Before public void before() { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); when(envVarUtil.replaceEnvironmentVariablePlaceholders(anyString())).thenCallRealMethod(); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_getValue_existing() throws Exception { when(envVarUtil.getValue(anyString())).thenCallRealMethod(); diff --git a/src/test/java/com/hivemq/websocket/WebSocketInitializerTest.java b/src/test/java/com/hivemq/websocket/WebSocketInitializerTest.java index 2de233b8d..6c3daaa49 100644 --- a/src/test/java/com/hivemq/websocket/WebSocketInitializerTest.java +++ b/src/test/java/com/hivemq/websocket/WebSocketInitializerTest.java @@ -18,6 +18,7 @@ import com.google.common.collect.Lists; import com.hivemq.configuration.service.entity.WebsocketListener; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockitoAnnotations; @@ -38,12 +39,12 @@ public class WebSocketInitializerTest { private EmbeddedChannel channel; - private WebsocketListener websocketListener; + private AutoCloseable closeable; @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + closeable = MockitoAnnotations.openMocks(this); channel = new EmbeddedChannel(); channel.pipeline().addLast("dummy", new DummyHandler()); @@ -51,6 +52,11 @@ public void setUp() throws Exception { websocketListener = new WebsocketListener.Builder().port(8000).bindAddress("0.0.0.0").build(); } + @After + public void releaseMocks() throws Exception { + closeable. close(); + } + @Test public void test_handler_in_pipeline() throws Exception {