Migrate to BDD Mockito

Migrate all tests to consistently use BDD Mockito. Also add
checksyle rule to enforce going forwards.
This commit is contained in:
Phillip Webb
2019-05-08 13:45:42 -07:00
parent 816bbee8de
commit 798b51f4a3
55 changed files with 322 additions and 324 deletions

View File

@@ -47,7 +47,7 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.BDDMockito.willAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -80,10 +80,10 @@ public class GenericMessagingTemplateTests {
public void sendWithTimeout() {
SubscribableChannel channel = mock(SubscribableChannel.class);
final AtomicReference<Message<?>> sent = new AtomicReference<>();
doAnswer(invocation -> {
willAnswer(invocation -> {
sent.set(invocation.getArgument(0));
return true;
}).when(channel).send(any(Message.class), eq(30_000L));
}).given(channel).send(any(Message.class), eq(30_000L));
Message<?> message = MessageBuilder.withPayload("request")
.setHeader(GenericMessagingTemplate.DEFAULT_SEND_TIMEOUT_HEADER, 30_000L)
.setHeader(GenericMessagingTemplate.DEFAULT_RECEIVE_TIMEOUT_HEADER, 1L)
@@ -99,10 +99,10 @@ public class GenericMessagingTemplateTests {
public void sendWithTimeoutMutable() {
SubscribableChannel channel = mock(SubscribableChannel.class);
final AtomicReference<Message<?>> sent = new AtomicReference<>();
doAnswer(invocation -> {
willAnswer(invocation -> {
sent.set(invocation.getArgument(0));
return true;
}).when(channel).send(any(Message.class), eq(30_000L));
}).given(channel).send(any(Message.class), eq(30_000L));
MessageHeaderAccessor accessor = new MessageHeaderAccessor();
accessor.setLeaveMutable(true);
Message<?> message = new GenericMessage<>("request", accessor.getMessageHeaders());
@@ -140,10 +140,10 @@ public class GenericMessagingTemplateTests {
SubscribableChannel channel = mock(SubscribableChannel.class);
MessageHandler handler = createLateReplier(latch, failure);
doAnswer(invocation -> {
willAnswer(invocation -> {
this.executor.execute(() -> handler.handleMessage(invocation.getArgument(0)));
return true;
}).when(channel).send(any(Message.class), anyLong());
}).given(channel).send(any(Message.class), anyLong());
assertNull(this.template.convertSendAndReceive(channel, "request", String.class));
assertTrue(latch.await(10_000, TimeUnit.MILLISECONDS));
@@ -166,10 +166,10 @@ public class GenericMessagingTemplateTests {
SubscribableChannel channel = mock(SubscribableChannel.class);
MessageHandler handler = createLateReplier(latch, failure);
doAnswer(invocation -> {
willAnswer(invocation -> {
this.executor.execute(() -> handler.handleMessage(invocation.getArgument(0)));
return true;
}).when(channel).send(any(Message.class), anyLong());
}).given(channel).send(any(Message.class), anyLong());
Message<?> message = MessageBuilder.withPayload("request")
.setHeader(GenericMessagingTemplate.DEFAULT_SEND_TIMEOUT_HEADER, 30_000L)
@@ -198,10 +198,10 @@ public class GenericMessagingTemplateTests {
SubscribableChannel channel = mock(SubscribableChannel.class);
MessageHandler handler = createLateReplier(latch, failure);
doAnswer(invocation -> {
willAnswer(invocation -> {
this.executor.execute(() -> handler.handleMessage(invocation.getArgument(0)));
return true;
}).when(channel).send(any(Message.class), anyLong());
}).given(channel).send(any(Message.class), anyLong());
Message<?> message = MessageBuilder.withPayload("request")
.setHeader("sto", 30_000L)

View File

@@ -36,8 +36,8 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Unit tests for {@link MessageMethodArgumentResolver}.
@@ -96,7 +96,7 @@ public class MessageMethodArgumentResolverTests {
Message<String> message = MessageBuilder.withPayload("test").build();
MethodParameter parameter = new MethodParameter(this.method, 1);
when(this.converter.fromMessage(message, Integer.class)).thenReturn(4);
given(this.converter.fromMessage(message, Integer.class)).willReturn(4);
@SuppressWarnings("unchecked")
Message<Integer> actual = (Message<Integer>) this.resolver.resolveArgument(parameter, message);

View File

@@ -30,10 +30,10 @@ import reactor.core.publisher.Mono;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
/**
* Unit tests for {@link DefaultRSocketRequesterBuilder}.
@@ -48,7 +48,7 @@ public class DefaultRSocketRequesterBuilderTests {
@Before
public void setup() {
this.transport = mock(ClientTransport.class);
when(this.transport.connect(anyInt())).thenReturn(Mono.just(new MockConnection()));
given(this.transport.connect(anyInt())).willReturn(Mono.just(new MockConnection()));
}

View File

@@ -46,13 +46,13 @@ import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
/**
* Unit tests for {@link SimpleBrokerMessageHandler}.
@@ -186,7 +186,7 @@ public class SimpleBrokerMessageHandlerTests {
@Test
public void startAndStopWithHeartbeatValue() {
ScheduledFuture future = mock(ScheduledFuture.class);
when(this.taskScheduler.scheduleWithFixedDelay(any(Runnable.class), eq(15000L))).thenReturn(future);
given(this.taskScheduler.scheduleWithFixedDelay(any(Runnable.class), eq(15000L))).willReturn(future);
this.messageHandler.setTaskScheduler(this.taskScheduler);
this.messageHandler.setHeartbeatValue(new long[] {15000, 16000});

View File

@@ -56,11 +56,11 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.notNull;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
/**
* Unit tests for {@link DefaultStompSession}.
@@ -94,7 +94,7 @@ public class DefaultStompSessionTests {
SettableListenableFuture<Void> future = new SettableListenableFuture<>();
future.set(null);
when(this.connection.send(this.messageCaptor.capture())).thenReturn(future);
given(this.connection.send(this.messageCaptor.capture())).willReturn(future);
}
@@ -236,7 +236,7 @@ public class DefaultStompSessionTests {
String payload = "Oops";
StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders());
when(this.sessionHandler.getPayloadType(stompHeaders)).thenReturn(String.class);
given(this.sessionHandler.getPayloadType(stompHeaders)).willReturn(String.class);
this.session.handleMessage(MessageBuilder.createMessage(
payload.getBytes(StandardCharsets.UTF_8), accessor.getMessageHeaders()));
@@ -267,7 +267,7 @@ public class DefaultStompSessionTests {
byte[] payload = "{'foo':'bar'}".getBytes(StandardCharsets.UTF_8);
StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders());
when(this.sessionHandler.getPayloadType(stompHeaders)).thenReturn(Map.class);
given(this.sessionHandler.getPayloadType(stompHeaders)).willReturn(Map.class);
this.session.handleMessage(MessageBuilder.createMessage(payload, accessor.getMessageHeaders()));
@@ -294,7 +294,7 @@ public class DefaultStompSessionTests {
String payload = "sample payload";
StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders());
when(frameHandler.getPayloadType(stompHeaders)).thenReturn(String.class);
given(frameHandler.getPayloadType(stompHeaders)).willReturn(String.class);
this.session.handleMessage(MessageBuilder.createMessage(payload.getBytes(StandardCharsets.UTF_8),
accessor.getMessageHeaders()));
@@ -322,7 +322,7 @@ public class DefaultStompSessionTests {
byte[] payload = "{'foo':'bar'}".getBytes(StandardCharsets.UTF_8);
StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders());
when(frameHandler.getPayloadType(stompHeaders)).thenReturn(Map.class);
given(frameHandler.getPayloadType(stompHeaders)).willReturn(Map.class);
this.session.handleMessage(MessageBuilder.createMessage(payload, accessor.getMessageHeaders()));
@@ -419,7 +419,7 @@ public class DefaultStompSessionTests {
SettableListenableFuture<Void> future = new SettableListenableFuture<>();
future.setException(exception);
when(this.connection.send(any())).thenReturn(future);
given(this.connection.send(any())).willReturn(future);
assertThatExceptionOfType(MessageDeliveryException.class).isThrownBy(() ->
this.session.send("/topic/foo", "sample payload".getBytes(StandardCharsets.UTF_8)))
.withCause(exception);
@@ -609,7 +609,7 @@ public class DefaultStompSessionTests {
AtomicReference<Boolean> notReceived = new AtomicReference<>();
ScheduledFuture future = mock(ScheduledFuture.class);
when(taskScheduler.schedule(any(Runnable.class), any(Date.class))).thenReturn(future);
given(taskScheduler.schedule(any(Runnable.class), any(Date.class))).willReturn(future);
StompHeaders headers = new StompHeaders();
headers.setDestination("/topic/foo");

View File

@@ -30,8 +30,8 @@ import org.springframework.util.StringUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Unit tests for
@@ -52,7 +52,7 @@ public class DefaultUserDestinationResolverTests {
simpUser.addSessions(new TestSimpSession("123"));
this.registry = mock(SimpUserRegistry.class);
when(this.registry.getUser("joe")).thenReturn(simpUser);
given(this.registry.getUser("joe")).willReturn(simpUser);
this.resolver = new DefaultUserDestinationResolver(this.registry);
}
@@ -91,7 +91,7 @@ public class DefaultUserDestinationResolverTests {
TestSimpUser simpUser = new TestSimpUser("joe");
simpUser.addSessions(new TestSimpSession("123"), new TestSimpSession("456"));
when(this.registry.getUser("joe")).thenReturn(simpUser);
given(this.registry.getUser("joe")).willReturn(simpUser);
TestPrincipal user = new TestPrincipal("joe");
Message<?> message = createMessage(SimpMessageType.SUBSCRIBE, user, "456", "/user/queue/foo");
@@ -157,7 +157,7 @@ public class DefaultUserDestinationResolverTests {
TestSimpUser otherSimpUser = new TestSimpUser("anna");
otherSimpUser.addSessions(new TestSimpSession("456"));
when(this.registry.getUser("anna")).thenReturn(otherSimpUser);
given(this.registry.getUser("anna")).willReturn(otherSimpUser);
TestPrincipal user = new TestPrincipal("joe");
TestPrincipal otherUser = new TestPrincipal("anna");
@@ -179,7 +179,7 @@ public class DefaultUserDestinationResolverTests {
TestSimpUser simpUser = new TestSimpUser(userName);
simpUser.addSessions(new TestSimpSession("openid123"));
when(this.registry.getUser(userName)).thenReturn(simpUser);
given(this.registry.getUser(userName)).willReturn(simpUser);
String destination = "/user/" + StringUtils.replace(userName, "/", "%2F") + "/queue/foo";

View File

@@ -36,8 +36,8 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Unit tests for {@link MultiServerUserRegistry}.
@@ -65,9 +65,9 @@ public class MultiServerUserRegistryTests {
public void getUserFromLocalRegistry() throws Exception {
SimpUser user = Mockito.mock(SimpUser.class);
Set<SimpUser> users = Collections.singleton(user);
when(this.localRegistry.getUsers()).thenReturn(users);
when(this.localRegistry.getUserCount()).thenReturn(1);
when(this.localRegistry.getUser("joe")).thenReturn(user);
given(this.localRegistry.getUsers()).willReturn(users);
given(this.localRegistry.getUserCount()).willReturn(1);
given(this.localRegistry.getUser("joe")).willReturn(user);
assertEquals(1, this.registry.getUserCount());
assertSame(user, this.registry.getUser("joe"));
@@ -81,7 +81,7 @@ public class MultiServerUserRegistryTests {
testSession.addSubscriptions(new TestSimpSubscription("remote-sub", "/remote-dest"));
testUser.addSessions(testSession);
SimpUserRegistry testRegistry = mock(SimpUserRegistry.class);
when(testRegistry.getUsers()).thenReturn(Collections.singleton(testUser));
given(testRegistry.getUsers()).willReturn(Collections.singleton(testUser));
Object registryDto = new MultiServerUserRegistry(testRegistry).getLocalRegistryDto();
Message<?> message = this.converter.toMessage(registryDto, null);
@@ -120,7 +120,7 @@ public class MultiServerUserRegistryTests {
user2.addSessions(session2);
user3.addSessions(session3);
SimpUserRegistry userRegistry = mock(SimpUserRegistry.class);
when(userRegistry.getUsers()).thenReturn(new HashSet<>(Arrays.asList(user1, user2, user3)));
given(userRegistry.getUsers()).willReturn(new HashSet<>(Arrays.asList(user1, user2, user3)));
Object registryDto = new MultiServerUserRegistry(userRegistry).getLocalRegistryDto();
Message<?> message = this.converter.toMessage(registryDto, null);
@@ -143,14 +143,14 @@ public class MultiServerUserRegistryTests {
TestSimpUser localUser = new TestSimpUser("joe");
TestSimpSession localSession = new TestSimpSession("sess123");
localUser.addSessions(localSession);
when(this.localRegistry.getUser("joe")).thenReturn(localUser);
given(this.localRegistry.getUser("joe")).willReturn(localUser);
// Prepare broadcast message from remote server
TestSimpUser remoteUser = new TestSimpUser("joe");
TestSimpSession remoteSession = new TestSimpSession("sess456");
remoteUser.addSessions(remoteSession);
SimpUserRegistry remoteRegistry = mock(SimpUserRegistry.class);
when(remoteRegistry.getUsers()).thenReturn(Collections.singleton(remoteUser));
given(remoteRegistry.getUsers()).willReturn(Collections.singleton(remoteUser));
Object remoteRegistryDto = new MultiServerUserRegistry(remoteRegistry).getLocalRegistryDto();
Message<?> message = this.converter.toMessage(remoteRegistryDto, null);
@@ -179,7 +179,7 @@ public class MultiServerUserRegistryTests {
TestSimpUser testUser = new TestSimpUser("joe");
testUser.addSessions(new TestSimpSession("remote-sub"));
SimpUserRegistry testRegistry = mock(SimpUserRegistry.class);
when(testRegistry.getUsers()).thenReturn(Collections.singleton(testUser));
given(testRegistry.getUsers()).willReturn(Collections.singleton(testUser));
Object registryDto = new MultiServerUserRegistry(testRegistry).getLocalRegistryDto();
Message<?> message = this.converter.toMessage(registryDto, null);

View File

@@ -39,7 +39,6 @@ import static org.junit.Assert.assertNotNull;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.springframework.messaging.simp.SimpMessageHeaderAccessor.ORIGINAL_DESTINATION;
/**
@@ -94,7 +93,7 @@ public class UserDestinationMessageHandlerTests {
public void handleMessage() {
TestSimpUser simpUser = new TestSimpUser("joe");
simpUser.addSessions(new TestSimpSession("123"));
when(this.registry.getUser("joe")).thenReturn(simpUser);
given(this.registry.getUser("joe")).willReturn(simpUser);
given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
this.handler.handleMessage(createWith(SimpMessageType.MESSAGE, "joe", "123", "/user/joe/queue/foo"));
@@ -130,7 +129,7 @@ public class UserDestinationMessageHandlerTests {
public void handleMessageFromBrokerWithActiveSession() {
TestSimpUser simpUser = new TestSimpUser("joe");
simpUser.addSessions(new TestSimpSession("123"));
when(this.registry.getUser("joe")).thenReturn(simpUser);
given(this.registry.getUser("joe")).willReturn(simpUser);
this.handler.setBroadcastDestination("/topic/unresolved");
given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);

View File

@@ -42,10 +42,10 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
/**
* User tests for {@link UserRegistryMessageHandler}.
@@ -73,7 +73,7 @@ public class UserRegistryMessageHandlerTests {
MockitoAnnotations.initMocks(this);
when(this.brokerChannel.send(any())).thenReturn(true);
given(this.brokerChannel.send(any())).willReturn(true);
this.converter = new MappingJackson2MessageConverter();
SimpMessagingTemplate brokerTemplate = new SimpMessagingTemplate(this.brokerChannel);
@@ -97,7 +97,7 @@ public class UserRegistryMessageHandlerTests {
public void brokerUnavailableEvent() throws Exception {
ScheduledFuture future = Mockito.mock(ScheduledFuture.class);
when(this.taskScheduler.scheduleWithFixedDelay(any(Runnable.class), any(Long.class))).thenReturn(future);
given(this.taskScheduler.scheduleWithFixedDelay(any(Runnable.class), any(Long.class))).willReturn(future);
BrokerAvailabilityEvent event = new BrokerAvailabilityEvent(true, this);
this.handler.onApplicationEvent(event);
@@ -118,7 +118,7 @@ public class UserRegistryMessageHandlerTests {
simpUser1.addSessions(new TestSimpSession("456"));
HashSet<SimpUser> simpUsers = new HashSet<>(Arrays.asList(simpUser1, simpUser2));
when(this.localRegistry.getUsers()).thenReturn(simpUsers);
given(this.localRegistry.getUsers()).willReturn(simpUsers);
getUserRegistryTask().run();
@@ -148,8 +148,8 @@ public class UserRegistryMessageHandlerTests {
HashSet<SimpUser> simpUsers = new HashSet<>(Arrays.asList(simpUser1, simpUser2));
SimpUserRegistry remoteUserRegistry = mock(SimpUserRegistry.class);
when(remoteUserRegistry.getUserCount()).thenReturn(2);
when(remoteUserRegistry.getUsers()).thenReturn(simpUsers);
given(remoteUserRegistry.getUserCount()).willReturn(2);
given(remoteUserRegistry.getUsers()).willReturn(simpUsers);
MultiServerUserRegistry remoteRegistry = new MultiServerUserRegistry(remoteUserRegistry);
Message<?> message = this.converter.toMessage(remoteRegistry.getLocalRegistryDto(), null);
@@ -166,8 +166,8 @@ public class UserRegistryMessageHandlerTests {
TestSimpUser simpUser = new TestSimpUser("joe");
simpUser.addSessions(new TestSimpSession("123"));
when(this.localRegistry.getUserCount()).thenReturn(1);
when(this.localRegistry.getUsers()).thenReturn(Collections.singleton(simpUser));
given(this.localRegistry.getUserCount()).willReturn(1);
given(this.localRegistry.getUsers()).willReturn(Collections.singleton(simpUser));
assertEquals(1, this.multiServerRegistry.getUserCount());