Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Deprecated MockitoAnnotations.initMocks #542

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/test/java/com/hivemq/bootstrap/BindInformationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -55,4 +62,4 @@ public void test_listener_null() throws Exception {
public void test_future_null() throws Exception {
new BindInformation(listener, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,6 +53,7 @@
public class HiveMQNettyBootstrapTest {

private HiveMQNettyBootstrap hiveMQNettyBootstrap;
private AutoCloseable closeable;

@Mock
private ShutdownHooks shutdownHooks;
Expand All @@ -73,7 +75,7 @@ public class HiveMQNettyBootstrapTest {

@Before
public void before() {
MockitoAnnotations.initMocks(this);
closeable = MockitoAnnotations.openMocks(this);
hiveMQNettyBootstrap = new HiveMQNettyBootstrap(shutdownHooks,
listenerConfigurationService,
channelInitializerFactoryImpl,
Expand All @@ -87,6 +89,11 @@ public void before() {
abstractChannelInitializer);
}

@After
public void releaseMocks() throws Exception {
closeable. close();
}

@Test
public void bootstrapServer_whenNoListenersProvided_thenSuccessfulBootstrap() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,17 +70,23 @@ 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);
channelInitializerFactory =
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -117,6 +119,11 @@ public void before() throws Exception {

}

@After
public void releaseMocks() throws Exception {
closeable. close();
}

@Test
public void test_add_special_handlers() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,17 +43,23 @@ 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
clientConnection.setProtocolVersion(ProtocolVersion.MQTTv5);
channel.attr(ClientConnectionContext.CHANNEL_ATTRIBUTE_NAME).set(clientConnection);
}

@After
public void releaseMocks() throws Exception {
closeable. close();
}

/* ***********************
* Test invalid messages *
* ***********************/
Expand Down
Loading