Polish mockito usage

Consistent use of BDDMockito rather than standard Mockito.
This commit is contained in:
Phillip Webb
2014-08-11 11:17:46 -07:00
parent 4db258b9e3
commit ac8326d2df
37 changed files with 317 additions and 354 deletions

View File

@@ -19,7 +19,7 @@ package org.springframework.messaging.core;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.mockito.BDDMockito.*;
/**
* Unit tests for {@link CachingDestinationResolverProxy}.
@@ -32,11 +32,11 @@ public class CachingDestinationResolverTests {
@Test
public void cachedDestination() {
@SuppressWarnings("unchecked")
DestinationResolver<String> destinationResolver = (DestinationResolver<String>) mock(DestinationResolver.class);
DestinationResolver<String> destinationResolver = mock(DestinationResolver.class);
CachingDestinationResolverProxy<String> cachingDestinationResolver = new CachingDestinationResolverProxy<String>(destinationResolver);
when(destinationResolver.resolveDestination("abcd")).thenReturn("dcba");
when(destinationResolver.resolveDestination("1234")).thenReturn("4321");
given(destinationResolver.resolveDestination("abcd")).willReturn("dcba");
given(destinationResolver.resolveDestination("1234")).willReturn("4321");
assertEquals("dcba", cachingDestinationResolver.resolveDestination("abcd"));
assertEquals("4321", cachingDestinationResolver.resolveDestination("1234"));

View File

@@ -20,14 +20,14 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.ObjectFactory;
import static org.mockito.BDDMockito.*;
import java.util.concurrent.ConcurrentHashMap;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.*;
/**
* Unit tests for {@link org.springframework.messaging.simp.SimpSessionScope}.
@@ -67,7 +67,7 @@ public class SimpSessionScopeTests {
@Test
public void getWithObjectFactory() {
when(this.objectFactory.getObject()).thenReturn("value");
given(this.objectFactory.getObject()).willReturn("value");
Object actual = this.scope.get("name", this.objectFactory);
assertThat(actual, is("value"));

View File

@@ -24,13 +24,11 @@ import javax.security.auth.Subject;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.core.MethodParameter;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
@@ -48,7 +46,7 @@ import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.util.MimeType;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.mockito.BDDMockito.*;
/**
* Test fixture for {@link SendToMethodReturnValueHandlerTests}.
@@ -125,7 +123,7 @@ public class SendToMethodReturnValueHandlerTests {
@Test
public void sendToNoAnnotations() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
given(this.messageChannel.send(any(Message.class))).willReturn(true);
Message<?> inputMessage = createInputMessage("sess1", "sub1", "/app", "/dest", null);
this.handler.handleReturnValue(PAYLOAD, this.noAnnotationsReturnType, inputMessage);
@@ -143,7 +141,7 @@ public class SendToMethodReturnValueHandlerTests {
@Test
public void sendTo() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
given(this.messageChannel.send(any(Message.class))).willReturn(true);
String sessionId = "sess1";
Message<?> inputMessage = createInputMessage(sessionId, "sub1", null, null, null);
@@ -169,7 +167,7 @@ public class SendToMethodReturnValueHandlerTests {
@Test
public void sendToDefaultDestination() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
given(this.messageChannel.send(any(Message.class))).willReturn(true);
String sessionId = "sess1";
Message<?> inputMessage = createInputMessage(sessionId, "sub1", "/app", "/dest", null);
@@ -188,7 +186,7 @@ public class SendToMethodReturnValueHandlerTests {
@Test
public void sendToDefaultDestinationWhenUsingDotPathSeparator() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
given(this.messageChannel.send(any(Message.class))).willReturn(true);
Message<?> inputMessage = createInputMessage("sess1", "sub1", "/app/", "dest.foo.bar", null);
this.handler.handleReturnValue(PAYLOAD, this.sendToDefaultDestReturnType, inputMessage);
@@ -225,7 +223,7 @@ public class SendToMethodReturnValueHandlerTests {
@Test
public void sendToUser() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
given(this.messageChannel.send(any(Message.class))).willReturn(true);
String sessionId = "sess1";
TestUser user = new TestUser();
@@ -250,7 +248,7 @@ public class SendToMethodReturnValueHandlerTests {
@Test
public void sendToUserSingleSession() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
given(this.messageChannel.send(any(Message.class))).willReturn(true);
String sessionId = "sess1";
TestUser user = new TestUser();
@@ -277,7 +275,7 @@ public class SendToMethodReturnValueHandlerTests {
@Test
public void sendToUserWithUserNameProvider() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
given(this.messageChannel.send(any(Message.class))).willReturn(true);
String sessionId = "sess1";
TestUser user = new UniqueUser();
@@ -296,7 +294,7 @@ public class SendToMethodReturnValueHandlerTests {
@Test
public void sendToUserDefaultDestination() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
given(this.messageChannel.send(any(Message.class))).willReturn(true);
String sessionId = "sess1";
TestUser user = new TestUser();
@@ -315,7 +313,7 @@ public class SendToMethodReturnValueHandlerTests {
@Test
public void sendToUserDefaultDestinationWhenUsingDotPathSeparator() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
given(this.messageChannel.send(any(Message.class))).willReturn(true);
TestUser user = new TestUser();
Message<?> inputMessage = createInputMessage("sess1", "sub1", "/app/", "dest.foo.bar", user);
@@ -331,7 +329,7 @@ public class SendToMethodReturnValueHandlerTests {
@Test
public void sendToUserDefaultDestinationSingleSession() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
given(this.messageChannel.send(any(Message.class))).willReturn(true);
String sessionId = "sess1";
TestUser user = new TestUser();
@@ -366,7 +364,7 @@ public class SendToMethodReturnValueHandlerTests {
@Test
public void sendToUserSessionWithoutUserName() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
given(this.messageChannel.send(any(Message.class))).willReturn(true);
String sessionId = "sess1";
Message<?> inputMessage = createInputMessage(sessionId, "sub1", null, null, null);
@@ -419,42 +417,35 @@ public class SendToMethodReturnValueHandlerTests {
}
}
@SuppressWarnings("unused")
public String handleNoAnnotations() {
return PAYLOAD;
}
@SuppressWarnings("unused")
@SendTo
public String handleAndSendToDefaultDestination() {
return PAYLOAD;
}
@SuppressWarnings("unused")
@SendTo({"/dest1", "/dest2"})
public String handleAndSendTo() {
return PAYLOAD;
}
@SuppressWarnings("unused")
@SendToUser
public String handleAndSendToUserDefaultDestination() {
return PAYLOAD;
}
@SuppressWarnings("unused")
@SendToUser(broadcast=false)
public String handleAndSendToUserDefaultDestinationSingleSession() {
return PAYLOAD;
}
@SuppressWarnings("unused")
@SendToUser({"/dest1", "/dest2"})
public String handleAndSendToUser() {
return PAYLOAD;
}
@SuppressWarnings("unused")
@SendToUser(value={"/dest1", "/dest2"}, broadcast=false)
public String handleAndSendToUserSingleSession() {
return PAYLOAD;

View File

@@ -43,7 +43,7 @@ import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.util.MimeType;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.mockito.BDDMockito.*;
/**
* Test fixture for {@link SubscriptionMethodReturnValueHandler}.
@@ -71,7 +71,6 @@ public class SubscriptionMethodReturnValueHandlerTests {
@Before
@SuppressWarnings({ "unchecked", "rawtypes" })
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
@@ -102,7 +101,7 @@ public class SubscriptionMethodReturnValueHandlerTests {
@Test
public void testMessageSentToChannel() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
given(this.messageChannel.send(any(Message.class))).willReturn(true);
String sessionId = "sess1";
String subscriptionId = "subs1";
@@ -162,20 +161,17 @@ public class SubscriptionMethodReturnValueHandlerTests {
}
@SuppressWarnings("unused")
@SubscribeMapping("/data") // not needed for the tests but here for completeness
private String getData() {
return PAYLOAD;
}
@SuppressWarnings("unused")
@SubscribeMapping("/data") // not needed for the tests but here for completeness
@SendTo("/sendToDest")
private String getDataAndSendTo() {
return PAYLOAD;
}
@SuppressWarnings("unused")
@MessageMapping("/handle") // not needed for the tests but here for completeness
public String handle() {
return PAYLOAD;

View File

@@ -30,8 +30,9 @@ import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.simp.TestPrincipal;
import org.springframework.messaging.support.MessageBuilder;
import static org.mockito.BDDMockito.*;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
/**
* Unit tests for {@link org.springframework.messaging.simp.user.UserDestinationMessageHandler}.
@@ -60,7 +61,7 @@ public class UserDestinationMessageHandlerTests {
@Test
@SuppressWarnings("rawtypes")
public void handleSubscribe() {
when(this.brokerChannel.send(Mockito.any(Message.class))).thenReturn(true);
given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
this.messageHandler.handleMessage(createMessage(SimpMessageType.SUBSCRIBE, "joe", SESSION_ID, "/user/queue/foo"));
ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
@@ -72,7 +73,7 @@ public class UserDestinationMessageHandlerTests {
@Test
@SuppressWarnings("rawtypes")
public void handleUnsubscribe() {
when(this.brokerChannel.send(Mockito.any(Message.class))).thenReturn(true);
given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
this.messageHandler.handleMessage(createMessage(SimpMessageType.UNSUBSCRIBE, "joe", "123", "/user/queue/foo"));
ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
@@ -85,7 +86,7 @@ public class UserDestinationMessageHandlerTests {
@SuppressWarnings("rawtypes")
public void handleMessage() {
this.registry.registerSessionId("joe", "123");
when(this.brokerChannel.send(Mockito.any(Message.class))).thenReturn(true);
given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
this.messageHandler.handleMessage(createMessage(SimpMessageType.MESSAGE, "joe", "123", "/user/joe/queue/foo"));
ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);

View File

@@ -16,6 +16,8 @@
package org.springframework.messaging.support;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -30,11 +32,8 @@ import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageDeliveryException;
import org.springframework.messaging.MessageHandler;
import java.util.concurrent.atomic.AtomicInteger;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.*;
/**
@@ -170,7 +169,7 @@ public class ExecutorSubscribableChannelTests {
@Test
public void interceptorWithException() {
IllegalStateException expected = new IllegalStateException("Fake exception");
doThrow(expected).when(this.handler).handleMessage(this.message);
willThrow(expected).given(this.handler).handleMessage(this.message);
BeforeHandleInterceptor interceptor = new BeforeHandleInterceptor();
this.channel.addInterceptor(interceptor);
this.channel.subscribe(this.handler);