diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheErrorHandlerTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheErrorHandlerTests.java index 34c4a8eb256..69b2dd16214 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheErrorHandlerTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheErrorHandlerTests.java @@ -16,11 +16,6 @@ package org.springframework.cache.jcache.interceptor; -import static org.mockito.BDDMockito.*; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - import java.util.Arrays; import java.util.concurrent.atomic.AtomicLong; @@ -35,7 +30,6 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; - import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; @@ -47,8 +41,9 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import static org.mockito.BDDMockito.*; + /** - * * @author Stephane Nicoll */ public class JCacheErrorHandlerTests { @@ -74,7 +69,7 @@ public class JCacheErrorHandlerTests { public void getFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get"); Object key = SimpleKeyGenerator.generateKey(0L); - doThrow(exception).when(cache).get(key); + willThrow(exception).given(cache).get(key); this.simpleService.get(0L); verify(errorHandler).handleCacheGetError(exception, cache, key); @@ -84,7 +79,7 @@ public class JCacheErrorHandlerTests { public void putFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on put"); Object key = SimpleKeyGenerator.generateKey(0L); - doThrow(exception).when(cache).put(key, 234L); + willThrow(exception).given(cache).put(key, 234L); this.simpleService.put(0L, 234L); verify(errorHandler).handleCachePutError(exception, cache, key, 234L); @@ -94,7 +89,7 @@ public class JCacheErrorHandlerTests { public void evictFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict"); Object key = SimpleKeyGenerator.generateKey(0L); - doThrow(exception).when(cache).evict(key); + willThrow(exception).given(cache).evict(key); this.simpleService.evict(0L); verify(errorHandler).handleCacheEvictError(exception, cache, key); @@ -103,7 +98,7 @@ public class JCacheErrorHandlerTests { @Test public void clearFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict"); - doThrow(exception).when(cache).clear(); + willThrow(exception).given(cache).clear(); this.simpleService.clear(); verify(errorHandler).handleCacheClearError(exception, cache); diff --git a/spring-context/src/test/java/org/springframework/cache/interceptor/CacheErrorHandlerTests.java b/spring-context/src/test/java/org/springframework/cache/interceptor/CacheErrorHandlerTests.java index 866a1afb427..72ad390c639 100644 --- a/spring-context/src/test/java/org/springframework/cache/interceptor/CacheErrorHandlerTests.java +++ b/spring-context/src/test/java/org/springframework/cache/interceptor/CacheErrorHandlerTests.java @@ -16,10 +16,6 @@ package org.springframework.cache.interceptor; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; -import static org.mockito.BDDMockito.*; - import java.util.Arrays; import java.util.concurrent.atomic.AtomicLong; @@ -27,7 +23,6 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; - import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CacheConfig; @@ -42,6 +37,10 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import static org.mockito.BDDMockito.*; + /** * * @author Stephane Nicoll @@ -71,7 +70,7 @@ public class CacheErrorHandlerTests { @Test public void getFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get"); - doThrow(exception).when(cache).get(0L); + willThrow(exception).given(cache).get(0L); Object result = this.simpleService.get(0L); verify(errorHandler).handleCacheGetError(exception, cache, 0L); @@ -82,12 +81,12 @@ public class CacheErrorHandlerTests { @Test public void getAndPutFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get"); - doThrow(exception).when(cache).get(0L); - doThrow(exception).when(cache).put(0L, 0L); // Update of the cache will fail as well + willThrow(exception).given(cache).get(0L); + willThrow(exception).given(cache).put(0L, 0L); // Update of the cache will fail as well Object counter = this.simpleService.get(0L); - doReturn(new SimpleValueWrapper(2L)).when(cache).get(0L); + willReturn(new SimpleValueWrapper(2L)).given(cache).get(0L); Object counter2 = this.simpleService.get(0L); Object counter3 = this.simpleService.get(0L); assertNotSame(counter, counter2); @@ -97,7 +96,7 @@ public class CacheErrorHandlerTests { @Test public void getFailProperException() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get"); - doThrow(exception).when(cache).get(0L); + willThrow(exception).given(cache).get(0L); cacheInterceptor.setErrorHandler(new SimpleCacheErrorHandler()); @@ -108,7 +107,7 @@ public class CacheErrorHandlerTests { @Test public void putFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on put"); - doThrow(exception).when(cache).put(0L, 0L); + willThrow(exception).given(cache).put(0L, 0L); this.simpleService.put(0L); verify(errorHandler).handleCachePutError(exception, cache, 0L, 0L); @@ -117,7 +116,7 @@ public class CacheErrorHandlerTests { @Test public void putFailProperException() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on put"); - doThrow(exception).when(cache).put(0L, 0L); + willThrow(exception).given(cache).put(0L, 0L); cacheInterceptor.setErrorHandler(new SimpleCacheErrorHandler()); @@ -128,7 +127,7 @@ public class CacheErrorHandlerTests { @Test public void evictFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict"); - doThrow(exception).when(cache).evict(0L); + willThrow(exception).given(cache).evict(0L); this.simpleService.evict(0L); verify(errorHandler).handleCacheEvictError(exception, cache, 0L); @@ -137,7 +136,7 @@ public class CacheErrorHandlerTests { @Test public void evictFailProperException() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict"); - doThrow(exception).when(cache).evict(0L); + willThrow(exception).given(cache).evict(0L); cacheInterceptor.setErrorHandler(new SimpleCacheErrorHandler()); @@ -148,7 +147,7 @@ public class CacheErrorHandlerTests { @Test public void clearFail() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict"); - doThrow(exception).when(cache).clear(); + willThrow(exception).given(cache).clear(); this.simpleService.clear(); verify(errorHandler).handleCacheClearError(exception, cache); @@ -157,7 +156,7 @@ public class CacheErrorHandlerTests { @Test public void clearFailProperException() { UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on evict"); - doThrow(exception).when(cache).clear(); + willThrow(exception).given(cache).clear(); cacheInterceptor.setErrorHandler(new SimpleCacheErrorHandler()); diff --git a/spring-context/src/test/java/org/springframework/context/event/ApplicationContextEventTests.java b/spring-context/src/test/java/org/springframework/context/event/ApplicationContextEventTests.java index 92e8b1cffd5..fa7b459bd57 100644 --- a/spring-context/src/test/java/org/springframework/context/event/ApplicationContextEventTests.java +++ b/spring-context/src/test/java/org/springframework/context/event/ApplicationContextEventTests.java @@ -22,7 +22,6 @@ import java.util.concurrent.Executor; import org.aopalliance.intercept.MethodInvocation; import org.junit.Test; - import org.springframework.aop.framework.ProxyFactory; import org.springframework.beans.factory.config.RuntimeBeanReference; import org.springframework.beans.factory.support.RootBeanDefinition; @@ -91,7 +90,7 @@ public class ApplicationContextEventTests { smc.addApplicationListener(listener); RuntimeException thrown = new RuntimeException(); - doThrow(thrown).when(listener).onApplicationEvent(evt); + willThrow(thrown).given(listener).onApplicationEvent(evt); try { smc.multicastEvent(evt); fail("Should have thrown RuntimeException"); @@ -111,7 +110,7 @@ public class ApplicationContextEventTests { smc.setErrorHandler(TaskUtils.LOG_AND_SUPPRESS_ERROR_HANDLER); smc.addApplicationListener(listener); - doThrow(new RuntimeException()).when(listener).onApplicationEvent(evt); + willThrow(new RuntimeException()).given(listener).onApplicationEvent(evt); smc.multicastEvent(evt); } diff --git a/spring-jms/src/test/java/org/springframework/jms/core/JmsMessagingTemplateTests.java b/spring-jms/src/test/java/org/springframework/jms/core/JmsMessagingTemplateTests.java index 2dcd718e6cc..53c6f7bd8fc 100644 --- a/spring-jms/src/test/java/org/springframework/jms/core/JmsMessagingTemplateTests.java +++ b/spring-jms/src/test/java/org/springframework/jms/core/JmsMessagingTemplateTests.java @@ -458,8 +458,8 @@ public class JmsMessagingTemplateTests { public void convertMessageConversionExceptionOnSend() throws JMSException { Message message = createTextMessage(); MessageConverter messageConverter = mock(MessageConverter.class); - doThrow(org.springframework.jms.support.converter.MessageConversionException.class) - .when(messageConverter).toMessage(eq(message), anyObject()); + willThrow(org.springframework.jms.support.converter.MessageConversionException.class) + .given(messageConverter).toMessage(eq(message), anyObject()); messagingTemplate.setJmsMessageConverter(messageConverter); invokeMessageCreator("myQueue"); @@ -471,8 +471,8 @@ public class JmsMessagingTemplateTests { public void convertMessageConversionExceptionOnReceive() throws JMSException { javax.jms.Message message = createJmsTextMessage(); MessageConverter messageConverter = mock(MessageConverter.class); - doThrow(org.springframework.jms.support.converter.MessageConversionException.class) - .when(messageConverter).fromMessage(message); + willThrow(org.springframework.jms.support.converter.MessageConversionException.class) + .given(messageConverter).fromMessage(message); messagingTemplate.setJmsMessageConverter(messageConverter); given(jmsTemplate.receive("myQueue")).willReturn(message); @@ -482,7 +482,7 @@ public class JmsMessagingTemplateTests { @Test public void convertMessageNotReadableException() throws JMSException { - doThrow(MessageNotReadableException.class).when(jmsTemplate).receive("myQueue"); + willThrow(MessageNotReadableException.class).given(jmsTemplate).receive("myQueue"); thrown.expect(MessagingException.class); messagingTemplate.receive("myQueue"); @@ -491,7 +491,7 @@ public class JmsMessagingTemplateTests { @Test public void convertDestinationResolutionExceptionOnSend() { Destination destination = new Destination() {}; - doThrow(DestinationResolutionException.class).when(jmsTemplate).send(eq(destination), anyObject()); + willThrow(DestinationResolutionException.class).given(jmsTemplate).send(eq(destination), anyObject()); thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class); messagingTemplate.send(destination, createTextMessage()); @@ -500,7 +500,7 @@ public class JmsMessagingTemplateTests { @Test public void convertDestinationResolutionExceptionOnReceive() { Destination destination = new Destination() {}; - doThrow(DestinationResolutionException.class).when(jmsTemplate).receive(destination); + willThrow(DestinationResolutionException.class).given(jmsTemplate).receive(destination); thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class); messagingTemplate.receive(destination); @@ -510,7 +510,7 @@ public class JmsMessagingTemplateTests { public void convertMessageFormatException() throws JMSException { Message message = createTextMessage(); MessageConverter messageConverter = mock(MessageConverter.class); - doThrow(MessageFormatException.class).when(messageConverter).toMessage(eq(message), anyObject()); + willThrow(MessageFormatException.class).given(messageConverter).toMessage(eq(message), anyObject()); messagingTemplate.setJmsMessageConverter(messageConverter); invokeMessageCreator("myQueue"); @@ -522,7 +522,7 @@ public class JmsMessagingTemplateTests { public void convertMessageNotWritableException() throws JMSException { Message message = createTextMessage(); MessageConverter messageConverter = mock(MessageConverter.class); - doThrow(MessageNotWriteableException.class).when(messageConverter).toMessage(eq(message), anyObject()); + willThrow(MessageNotWriteableException.class).given(messageConverter).toMessage(eq(message), anyObject()); messagingTemplate.setJmsMessageConverter(messageConverter); invokeMessageCreator("myQueue"); @@ -532,7 +532,7 @@ public class JmsMessagingTemplateTests { @Test public void convertInvalidDestinationExceptionOnSendAndReceiveWithName() { - doThrow(InvalidDestinationException.class).when(jmsTemplate).sendAndReceive(eq("unknownQueue"), anyObject()); + willThrow(InvalidDestinationException.class).given(jmsTemplate).sendAndReceive(eq("unknownQueue"), anyObject()); thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class); messagingTemplate.sendAndReceive("unknownQueue", createTextMessage()); @@ -541,21 +541,21 @@ public class JmsMessagingTemplateTests { @Test public void convertInvalidDestinationExceptionOnSendAndReceive() { Destination destination = new Destination() {}; - doThrow(InvalidDestinationException.class).when(jmsTemplate).sendAndReceive(eq(destination), anyObject()); + willThrow(InvalidDestinationException.class).given(jmsTemplate).sendAndReceive(eq(destination), anyObject()); thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class); messagingTemplate.sendAndReceive(destination, createTextMessage()); } private void invokeMessageCreator(String destinationName) { - doAnswer(new Answer() { + willAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { MessageCreator messageCreator = (MessageCreator) invocation.getArguments()[1]; messageCreator.createMessage(null); return null; } - }).when(jmsTemplate).send(eq("myQueue"), anyObject()); + }).given(jmsTemplate).send(eq("myQueue"), anyObject()); } diff --git a/spring-messaging/src/test/java/org/springframework/messaging/core/CachingDestinationResolverTests.java b/spring-messaging/src/test/java/org/springframework/messaging/core/CachingDestinationResolverTests.java index c31aaa38c82..49648082708 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/core/CachingDestinationResolverTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/core/CachingDestinationResolverTests.java @@ -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 destinationResolver = (DestinationResolver) mock(DestinationResolver.class); + DestinationResolver destinationResolver = mock(DestinationResolver.class); CachingDestinationResolverProxy cachingDestinationResolver = new CachingDestinationResolverProxy(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")); diff --git a/spring-messaging/src/test/java/org/springframework/messaging/simp/SimpSessionScopeTests.java b/spring-messaging/src/test/java/org/springframework/messaging/simp/SimpSessionScopeTests.java index 7494ece3146..dec5f11b3a7 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/simp/SimpSessionScopeTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/simp/SimpSessionScopeTests.java @@ -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")); diff --git a/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java index 865849ea99f..f74ab199839 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java @@ -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; diff --git a/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SubscriptionMethodReturnValueHandlerTests.java b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SubscriptionMethodReturnValueHandlerTests.java index 02b8b2e4996..424af87ffa5 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SubscriptionMethodReturnValueHandlerTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SubscriptionMethodReturnValueHandlerTests.java @@ -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; diff --git a/spring-messaging/src/test/java/org/springframework/messaging/simp/user/UserDestinationMessageHandlerTests.java b/spring-messaging/src/test/java/org/springframework/messaging/simp/user/UserDestinationMessageHandlerTests.java index c0544b33c77..491136e338e 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/simp/user/UserDestinationMessageHandlerTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/simp/user/UserDestinationMessageHandlerTests.java @@ -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 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 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 captor = ArgumentCaptor.forClass(Message.class); diff --git a/spring-messaging/src/test/java/org/springframework/messaging/support/ExecutorSubscribableChannelTests.java b/spring-messaging/src/test/java/org/springframework/messaging/support/ExecutorSubscribableChannelTests.java index 173829aa5d1..f2eeb094b1b 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/support/ExecutorSubscribableChannelTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/support/ExecutorSubscribableChannelTests.java @@ -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); diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListenerTests.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListenerTests.java index 45828221408..b6d742182a1 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListenerTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListenerTests.java @@ -17,7 +17,7 @@ package org.springframework.test.context.jdbc; import org.junit.Test; -import org.mockito.Mockito; +import org.mockito.BDDMockito; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.core.io.Resource; @@ -25,7 +25,7 @@ import org.springframework.test.context.TestContext; import org.springframework.test.context.jdbc.SqlConfig.TransactionMode; import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.mockito.BDDMockito.*; /** * Unit tests for {@link SqlScriptsTestExecutionListener}. @@ -43,8 +43,8 @@ public class SqlScriptsTestExecutionListenerTests { @Test public void missingValueAndScriptsAtClassLevel() throws Exception { Class clazz = MissingValueAndScriptsAtClassLevel.class; - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); - when(testContext.getTestMethod()).thenReturn(clazz.getDeclaredMethod("foo")); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); + given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo")); assertExceptionContains(clazz.getSimpleName() + ".sql"); } @@ -52,8 +52,8 @@ public class SqlScriptsTestExecutionListenerTests { @Test public void missingValueAndScriptsAtMethodLevel() throws Exception { Class clazz = MissingValueAndScriptsAtMethodLevel.class; - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); - when(testContext.getTestMethod()).thenReturn(clazz.getDeclaredMethod("foo")); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); + given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo")); assertExceptionContains(clazz.getSimpleName() + ".foo" + ".sql"); } @@ -61,8 +61,8 @@ public class SqlScriptsTestExecutionListenerTests { @Test public void valueAndScriptsDeclared() throws Exception { Class clazz = ValueAndScriptsDeclared.class; - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); - when(testContext.getTestMethod()).thenReturn(clazz.getDeclaredMethod("foo")); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); + given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo")); assertExceptionContains("Only one declaration of SQL script paths is permitted"); } @@ -70,13 +70,13 @@ public class SqlScriptsTestExecutionListenerTests { @Test public void isolatedTxModeDeclaredWithoutTxMgr() throws Exception { ApplicationContext ctx = mock(ApplicationContext.class); - when(ctx.getResource(anyString())).thenReturn(mock(Resource.class)); - when(ctx.getAutowireCapableBeanFactory()).thenReturn(mock(AutowireCapableBeanFactory.class)); + given(ctx.getResource(anyString())).willReturn(mock(Resource.class)); + given(ctx.getAutowireCapableBeanFactory()).willReturn(mock(AutowireCapableBeanFactory.class)); Class clazz = IsolatedWithoutTxMgr.class; - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); - when(testContext.getTestMethod()).thenReturn(clazz.getDeclaredMethod("foo")); - when(testContext.getApplicationContext()).thenReturn(ctx); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); + given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo")); + given(testContext.getApplicationContext()).willReturn(ctx); assertExceptionContains("cannot execute SQL scripts using Transaction Mode [ISOLATED] without a PlatformTransactionManager"); } @@ -84,13 +84,13 @@ public class SqlScriptsTestExecutionListenerTests { @Test public void missingDataSourceAndTxMgr() throws Exception { ApplicationContext ctx = mock(ApplicationContext.class); - when(ctx.getResource(anyString())).thenReturn(mock(Resource.class)); - when(ctx.getAutowireCapableBeanFactory()).thenReturn(mock(AutowireCapableBeanFactory.class)); + given(ctx.getResource(anyString())).willReturn(mock(Resource.class)); + given(ctx.getAutowireCapableBeanFactory()).willReturn(mock(AutowireCapableBeanFactory.class)); Class clazz = MissingDataSourceAndTxMgr.class; - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); - when(testContext.getTestMethod()).thenReturn(clazz.getDeclaredMethod("foo")); - when(testContext.getApplicationContext()).thenReturn(ctx); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); + given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo")); + given(testContext.getApplicationContext()).willReturn(ctx); assertExceptionContains("supply at least a DataSource or PlatformTransactionManager"); } diff --git a/spring-test/src/test/java/org/springframework/test/context/support/DirtiesContextTestExecutionListenerTests.java b/spring-test/src/test/java/org/springframework/test/context/support/DirtiesContextTestExecutionListenerTests.java index b94049842db..561cda88d9d 100644 --- a/spring-test/src/test/java/org/springframework/test/context/support/DirtiesContextTestExecutionListenerTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/support/DirtiesContextTestExecutionListenerTests.java @@ -20,13 +20,13 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import org.junit.Test; -import org.mockito.Mockito; +import org.mockito.BDDMockito; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext.ClassMode; import org.springframework.test.annotation.DirtiesContext.HierarchyMode; import org.springframework.test.context.TestContext; -import static org.mockito.Mockito.*; +import static org.mockito.BDDMockito.*; import static org.springframework.test.annotation.DirtiesContext.ClassMode.*; import static org.springframework.test.annotation.DirtiesContext.HierarchyMode.*; @@ -45,8 +45,8 @@ public class DirtiesContextTestExecutionListenerTests { @Test public void afterTestMethodForDirtiesContextDeclaredLocallyOnMethod() throws Exception { Class clazz = getClass(); - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); - when(testContext.getTestMethod()).thenReturn(clazz.getDeclaredMethod("dirtiesContextDeclaredLocally")); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); + given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("dirtiesContextDeclaredLocally")); listener.afterTestMethod(testContext); verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE); } @@ -54,8 +54,8 @@ public class DirtiesContextTestExecutionListenerTests { @Test public void afterTestMethodForDirtiesContextDeclaredOnMethodViaMetaAnnotation() throws Exception { Class clazz = getClass(); - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); - when(testContext.getTestMethod()).thenReturn(clazz.getDeclaredMethod("dirtiesContextDeclaredViaMetaAnnotation")); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); + given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("dirtiesContextDeclaredViaMetaAnnotation")); listener.afterTestMethod(testContext); verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE); } @@ -63,8 +63,8 @@ public class DirtiesContextTestExecutionListenerTests { @Test public void afterTestMethodForDirtiesContextDeclaredLocallyOnClassAfterEachTestMethod() throws Exception { Class clazz = DirtiesContextDeclaredLocallyAfterEachTestMethod.class; - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); - when(testContext.getTestMethod()).thenReturn(clazz.getDeclaredMethod("clean")); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); + given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean")); listener.afterTestMethod(testContext); verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE); } @@ -72,8 +72,8 @@ public class DirtiesContextTestExecutionListenerTests { @Test public void afterTestMethodForDirtiesContextDeclaredViaMetaAnnotationOnClassAfterEachTestMethod() throws Exception { Class clazz = DirtiesContextDeclaredViaMetaAnnotationAfterEachTestMethod.class; - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); - when(testContext.getTestMethod()).thenReturn(clazz.getDeclaredMethod("clean")); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); + given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean")); listener.afterTestMethod(testContext); verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE); } @@ -81,8 +81,8 @@ public class DirtiesContextTestExecutionListenerTests { @Test public void afterTestMethodForDirtiesContextDeclaredLocallyOnClassAfterClass() throws Exception { Class clazz = DirtiesContextDeclaredLocallyAfterClass.class; - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); - when(testContext.getTestMethod()).thenReturn(clazz.getDeclaredMethod("clean")); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); + given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean")); listener.afterTestMethod(testContext); verify(testContext, times(0)).markApplicationContextDirty(EXHAUSTIVE); } @@ -90,8 +90,8 @@ public class DirtiesContextTestExecutionListenerTests { @Test public void afterTestMethodForDirtiesContextDeclaredViaMetaAnnotationOnClassAfterClass() throws Exception { Class clazz = DirtiesContextDeclaredViaMetaAnnotationAfterClass.class; - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); - when(testContext.getTestMethod()).thenReturn(clazz.getDeclaredMethod("clean")); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); + given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean")); listener.afterTestMethod(testContext); verify(testContext, times(0)).markApplicationContextDirty(EXHAUSTIVE); } @@ -99,8 +99,8 @@ public class DirtiesContextTestExecutionListenerTests { @Test public void afterTestMethodForDirtiesContextViaMetaAnnotationWithOverrides() throws Exception { Class clazz = DirtiesContextViaMetaAnnotationWithOverrides.class; - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); - when(testContext.getTestMethod()).thenReturn(clazz.getDeclaredMethod("clean")); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); + given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean")); listener.afterTestMethod(testContext); verify(testContext, times(1)).markApplicationContextDirty(CURRENT_LEVEL); } @@ -110,7 +110,7 @@ public class DirtiesContextTestExecutionListenerTests { @Test public void afterTestClassForDirtiesContextDeclaredLocallyOnMethod() throws Exception { Class clazz = getClass(); - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); listener.afterTestClass(testContext); verify(testContext, times(0)).markApplicationContextDirty(EXHAUSTIVE); } @@ -118,7 +118,7 @@ public class DirtiesContextTestExecutionListenerTests { @Test public void afterTestClassForDirtiesContextDeclaredLocallyOnClassAfterEachTestMethod() throws Exception { Class clazz = DirtiesContextDeclaredLocallyAfterEachTestMethod.class; - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); listener.afterTestClass(testContext); verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE); } @@ -126,7 +126,7 @@ public class DirtiesContextTestExecutionListenerTests { @Test public void afterTestClassForDirtiesContextDeclaredViaMetaAnnotationOnClassAfterEachTestMethod() throws Exception { Class clazz = DirtiesContextDeclaredViaMetaAnnotationAfterEachTestMethod.class; - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); listener.afterTestClass(testContext); verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE); } @@ -134,7 +134,7 @@ public class DirtiesContextTestExecutionListenerTests { @Test public void afterTestClassForDirtiesContextDeclaredLocallyOnClassAfterClass() throws Exception { Class clazz = DirtiesContextDeclaredLocallyAfterClass.class; - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); listener.afterTestClass(testContext); verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE); } @@ -142,7 +142,7 @@ public class DirtiesContextTestExecutionListenerTests { @Test public void afterTestClassForDirtiesContextDeclaredViaMetaAnnotationOnClassAfterClass() throws Exception { Class clazz = DirtiesContextDeclaredViaMetaAnnotationAfterClass.class; - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); listener.afterTestClass(testContext); verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE); } @@ -150,7 +150,7 @@ public class DirtiesContextTestExecutionListenerTests { @Test public void afterTestClassForDirtiesContextDeclaredViaMetaAnnotationWithOverrides() throws Exception { Class clazz = DirtiesContextViaMetaAnnotationWithOverrides.class; - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); listener.afterTestClass(testContext); verify(testContext, times(1)).markApplicationContextDirty(CURRENT_LEVEL); } @@ -158,7 +158,7 @@ public class DirtiesContextTestExecutionListenerTests { @Test public void afterTestClassForDirtiesContextDeclaredViaMetaAnnotationWithOverridenAttributes() throws Exception { Class clazz = DirtiesContextViaMetaAnnotationWithOverridenAttributes.class; - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); listener.afterTestClass(testContext); verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE); } diff --git a/spring-test/src/test/java/org/springframework/test/context/transaction/TransactionalTestExecutionListenerTests.java b/spring-test/src/test/java/org/springframework/test/context/transaction/TransactionalTestExecutionListenerTests.java index e2b2df69362..b4c92127ad4 100644 --- a/spring-test/src/test/java/org/springframework/test/context/transaction/TransactionalTestExecutionListenerTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/transaction/TransactionalTestExecutionListenerTests.java @@ -21,7 +21,7 @@ import java.lang.annotation.RetentionPolicy; import org.junit.After; import org.junit.Test; -import org.mockito.Mockito; +import org.mockito.BDDMockito; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.TestContext; import org.springframework.transaction.PlatformTransactionManager; @@ -31,7 +31,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.support.SimpleTransactionStatus; import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.mockito.BDDMockito.*; import static org.springframework.transaction.annotation.Propagation.*; /** @@ -65,10 +65,10 @@ public class TransactionalTestExecutionListenerTests { private void assertBeforeTestMethodWithTransactionalTestMethod(Class clazz, boolean invokedInTx) throws Exception { - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); Invocable instance = clazz.newInstance(); - when(testContext.getTestInstance()).thenReturn(instance); - when(testContext.getTestMethod()).thenReturn(clazz.getDeclaredMethod("transactionalTest")); + given(testContext.getTestInstance()).willReturn(instance); + given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("transactionalTest")); assertFalse(instance.invoked); TransactionContextHolder.removeCurrentTransactionContext(); @@ -78,10 +78,10 @@ public class TransactionalTestExecutionListenerTests { private void assertBeforeTestMethodWithNonTransactionalTestMethod(Class clazz) throws Exception { - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); Invocable instance = clazz.newInstance(); - when(testContext.getTestInstance()).thenReturn(instance); - when(testContext.getTestMethod()).thenReturn(clazz.getDeclaredMethod("nonTransactionalTest")); + given(testContext.getTestInstance()).willReturn(instance); + given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("nonTransactionalTest")); assertFalse(instance.invoked); TransactionContextHolder.removeCurrentTransactionContext(); @@ -95,12 +95,12 @@ public class TransactionalTestExecutionListenerTests { } private void assertAfterTestMethodWithTransactionalTestMethod(Class clazz) throws Exception { - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); Invocable instance = clazz.newInstance(); - when(testContext.getTestInstance()).thenReturn(instance); - when(testContext.getTestMethod()).thenReturn(clazz.getDeclaredMethod("transactionalTest")); + given(testContext.getTestInstance()).willReturn(instance); + given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("transactionalTest")); - when(tm.getTransaction(Mockito.any(TransactionDefinition.class))).thenReturn(new SimpleTransactionStatus()); + given(tm.getTransaction(BDDMockito.any(TransactionDefinition.class))).willReturn(new SimpleTransactionStatus()); assertFalse(instance.invoked); TransactionContextHolder.removeCurrentTransactionContext(); @@ -110,10 +110,10 @@ public class TransactionalTestExecutionListenerTests { } private void assertAfterTestMethodWithNonTransactionalTestMethod(Class clazz) throws Exception { - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); Invocable instance = clazz.newInstance(); - when(testContext.getTestInstance()).thenReturn(instance); - when(testContext.getTestMethod()).thenReturn(clazz.getDeclaredMethod("nonTransactionalTest")); + given(testContext.getTestInstance()).willReturn(instance); + given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("nonTransactionalTest")); assertFalse(instance.invoked); TransactionContextHolder.removeCurrentTransactionContext(); @@ -124,7 +124,7 @@ public class TransactionalTestExecutionListenerTests { private void assertTransactionConfigurationAttributes(Class clazz, String transactionManagerName, boolean defaultRollback) { - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); TransactionConfigurationAttributes attributes = listener.retrieveConfigurationAttributes(testContext); assertNotNull(attributes); @@ -133,8 +133,8 @@ public class TransactionalTestExecutionListenerTests { } private void assertIsRollback(Class clazz, boolean rollback) throws NoSuchMethodException, Exception { - Mockito.> when(testContext.getTestClass()).thenReturn(clazz); - when(testContext.getTestMethod()).thenReturn(clazz.getDeclaredMethod("test")); + BDDMockito.> given(testContext.getTestClass()).willReturn(clazz); + given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("test")); assertEquals(rollback, listener.isRollback(testContext)); } diff --git a/spring-test/src/test/java/org/springframework/test/context/web/ServletTestExecutionListenerTests.java b/spring-test/src/test/java/org/springframework/test/context/web/ServletTestExecutionListenerTests.java index 54b23941c94..23074dc1ac5 100644 --- a/spring-test/src/test/java/org/springframework/test/context/web/ServletTestExecutionListenerTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/web/ServletTestExecutionListenerTests.java @@ -18,7 +18,7 @@ package org.springframework.test.context.web; import org.junit.Before; import org.junit.Test; -import org.mockito.Mockito; +import org.mockito.BDDMockito; import org.springframework.context.ApplicationContext; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; @@ -29,8 +29,9 @@ import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletWebRequest; +import static org.mockito.BDDMockito.*; + import static org.junit.Assert.*; -import static org.mockito.Mockito.*; import static org.springframework.test.context.web.ServletTestExecutionListener.*; /** @@ -75,8 +76,8 @@ public class ServletTestExecutionListenerTests { @Before public void setUp() { - when(wac.getServletContext()).thenReturn(mockServletContext); - when(testContext.getApplicationContext()).thenReturn(wac); + given(wac.getServletContext()).willReturn(mockServletContext); + given(testContext.getApplicationContext()).willReturn(wac); MockHttpServletRequest request = new MockHttpServletRequest(mockServletContext); MockHttpServletResponse response = new MockHttpServletResponse(); @@ -90,8 +91,8 @@ public class ServletTestExecutionListenerTests { @Test public void standardApplicationContext() throws Exception { - Mockito.> when(testContext.getTestClass()).thenReturn(getClass()); - when(testContext.getApplicationContext()).thenReturn(mock(ApplicationContext.class)); + BDDMockito.> given(testContext.getTestClass()).willReturn(getClass()); + given(testContext.getApplicationContext()).willReturn(mock(ApplicationContext.class)); listener.beforeTestClass(testContext); assertAttributeExists(); @@ -108,7 +109,7 @@ public class ServletTestExecutionListenerTests { @Test public void legacyWebTestCaseWithoutExistingRequestAttributes() throws Exception { - Mockito.> when(testContext.getTestClass()).thenReturn(LegacyWebTestCase.class); + BDDMockito.> given(testContext.getTestClass()).willReturn(LegacyWebTestCase.class); RequestContextHolder.resetRequestAttributes(); assertAttributesNotAvailable(); @@ -118,7 +119,7 @@ public class ServletTestExecutionListenerTests { listener.prepareTestInstance(testContext); assertAttributesNotAvailable(); verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); - when(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).thenReturn(null); + given(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(null); listener.beforeTestMethod(testContext); assertAttributesNotAvailable(); @@ -131,7 +132,7 @@ public class ServletTestExecutionListenerTests { @Test public void legacyWebTestCaseWithPresetRequestAttributes() throws Exception { - Mockito.> when(testContext.getTestClass()).thenReturn(LegacyWebTestCase.class); + BDDMockito.> given(testContext.getTestClass()).willReturn(LegacyWebTestCase.class); listener.beforeTestClass(testContext); assertAttributeExists(); @@ -139,12 +140,12 @@ public class ServletTestExecutionListenerTests { listener.prepareTestInstance(testContext); assertAttributeExists(); verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); - when(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).thenReturn(null); + given(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(null); listener.beforeTestMethod(testContext); assertAttributeExists(); verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); - when(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).thenReturn(null); + given(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(null); listener.afterTestMethod(testContext); verify(testContext, times(1)).removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE); @@ -153,7 +154,7 @@ public class ServletTestExecutionListenerTests { @Test public void atWebAppConfigTestCaseWithoutExistingRequestAttributes() throws Exception { - Mockito.> when(testContext.getTestClass()).thenReturn(AtWebAppConfigWebTestCase.class); + BDDMockito.> given(testContext.getTestClass()).willReturn(AtWebAppConfigWebTestCase.class); RequestContextHolder.resetRequestAttributes(); listener.beforeTestClass(testContext); @@ -164,7 +165,7 @@ public class ServletTestExecutionListenerTests { @Test public void atWebAppConfigTestCaseWithPresetRequestAttributes() throws Exception { - Mockito.> when(testContext.getTestClass()).thenReturn(AtWebAppConfigWebTestCase.class); + BDDMockito.> given(testContext.getTestClass()).willReturn(AtWebAppConfigWebTestCase.class); listener.beforeTestClass(testContext); assertAttributesAvailable(); @@ -177,8 +178,8 @@ public class ServletTestExecutionListenerTests { assertAttributeDoesNotExist(); verify(testContext, times(1)).setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); verify(testContext, times(1)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); - when(testContext.getAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).thenReturn(Boolean.TRUE); - when(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).thenReturn(Boolean.TRUE); + given(testContext.getAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(Boolean.TRUE); + given(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(Boolean.TRUE); listener.beforeTestMethod(testContext); assertAttributeDoesNotExist(); diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java index c2eb2391acd..53e8a0df8da 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java @@ -34,14 +34,18 @@ import org.springframework.test.web.servlet.samples.context.JavaConfigTests.Root import org.springframework.test.web.servlet.samples.context.JavaConfigTests.WebConfig; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import org.springframework.web.servlet.config.annotation.*; +import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.tiles3.TilesConfigurer; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; - +import static org.mockito.BDDMockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -import static org.mockito.Mockito.*; /** * Tests with Java configuration. @@ -70,7 +74,7 @@ public class JavaConfigTests { @Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); - when(this.personDao.getPerson(5L)).thenReturn(new Person("Joe")); + given(this.personDao.getPerson(5L)).willReturn(new Person("Joe")); } @Test diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/XmlConfigTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/XmlConfigTests.java index e9091ef5b08..03fb087bfcc 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/XmlConfigTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/XmlConfigTests.java @@ -30,8 +30,7 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import static org.mockito.Mockito.*; - +import static org.mockito.BDDMockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @@ -62,7 +61,7 @@ public class XmlConfigTests { @Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); - when(this.personDao.getPerson(5L)).thenReturn(new Person("Joe")); + given(this.personDao.getPerson(5L)).willReturn(new Person("Joe")); } @Test diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverterTests.java index 91ffcfbf7b5..7881051af8b 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverterTests.java @@ -32,7 +32,7 @@ import org.springframework.oxm.Unmarshaller; import org.springframework.oxm.UnmarshallingFailureException; import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.mockito.BDDMockito.*; /** * Tests for {@link MarshallingHttpMessageConverter}. @@ -45,8 +45,8 @@ public class MarshallingHttpMessageConverterTests { public void canRead() throws Exception { Unmarshaller unmarshaller = mock(Unmarshaller.class); - when(unmarshaller.supports(Integer.class)).thenReturn(false); - when(unmarshaller.supports(String.class)).thenReturn(true); + given(unmarshaller.supports(Integer.class)).willReturn(false); + given(unmarshaller.supports(String.class)).willReturn(true); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); converter.setUnmarshaller(unmarshaller); @@ -60,8 +60,8 @@ public class MarshallingHttpMessageConverterTests { public void canWrite() throws Exception { Marshaller marshaller = mock(Marshaller.class); - when(marshaller.supports(Integer.class)).thenReturn(false); - when(marshaller.supports(String.class)).thenReturn(true); + given(marshaller.supports(Integer.class)).willReturn(false); + given(marshaller.supports(String.class)).willReturn(true); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); converter.setMarshaller(marshaller); @@ -77,7 +77,7 @@ public class MarshallingHttpMessageConverterTests { MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); Unmarshaller unmarshaller = mock(Unmarshaller.class); - when(unmarshaller.unmarshal(isA(StreamSource.class))).thenReturn(body); + given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(body); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); converter.setUnmarshaller(unmarshaller); @@ -92,7 +92,7 @@ public class MarshallingHttpMessageConverterTests { Marshaller marshaller = mock(Marshaller.class); Unmarshaller unmarshaller = mock(Unmarshaller.class); - when(unmarshaller.unmarshal(isA(StreamSource.class))).thenReturn(Integer.valueOf(3)); + given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(Integer.valueOf(3)); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller, unmarshaller); converter.read(String.class, inputMessage); @@ -104,7 +104,7 @@ public class MarshallingHttpMessageConverterTests { UnmarshallingFailureException ex = new UnmarshallingFailureException("forced"); Unmarshaller unmarshaller = mock(Unmarshaller.class); - when(unmarshaller.unmarshal(isA(StreamSource.class))).thenThrow(ex); + given(unmarshaller.unmarshal(isA(StreamSource.class))).willThrow(ex); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); converter.setUnmarshaller(unmarshaller); @@ -124,7 +124,7 @@ public class MarshallingHttpMessageConverterTests { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); Marshaller marshaller = mock(Marshaller.class); - doNothing().when(marshaller).marshal(eq(body), isA(Result.class)); + willDoNothing().given(marshaller).marshal(eq(body), isA(Result.class)); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller); converter.write(body, null, outputMessage); @@ -140,7 +140,7 @@ public class MarshallingHttpMessageConverterTests { MarshallingFailureException ex = new MarshallingFailureException("forced"); Marshaller marshaller = mock(Marshaller.class); - doThrow(ex).when(marshaller).marshal(eq(body), isA(Result.class)); + willThrow(ex).given(marshaller).marshal(eq(body), isA(Result.class)); try { MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller); diff --git a/spring-web/src/test/java/org/springframework/web/context/request/ServletRequestAttributesTests.java b/spring-web/src/test/java/org/springframework/web/context/request/ServletRequestAttributesTests.java index 226b05d3d1e..1b7bd416d16 100644 --- a/spring-web/src/test/java/org/springframework/web/context/request/ServletRequestAttributesTests.java +++ b/spring-web/src/test/java/org/springframework/web/context/request/ServletRequestAttributesTests.java @@ -18,11 +18,11 @@ package org.springframework.web.context.request; import java.io.Serializable; import java.math.BigInteger; + import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.junit.Test; - import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.mock.web.test.MockHttpSession; @@ -157,8 +157,8 @@ public class ServletRequestAttributesTests { public void updateAccessedAttributes() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); HttpSession session = mock(HttpSession.class); - when(request.getSession(anyBoolean())).thenReturn(session); - when(session.getAttribute(KEY)).thenReturn(VALUE); + given(request.getSession(anyBoolean())).willReturn(session); + given(session.getAttribute(KEY)).willReturn(VALUE); ServletRequestAttributes attrs = new ServletRequestAttributes(request); assertSame(VALUE, attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION)); @@ -202,8 +202,8 @@ public class ServletRequestAttributesTests { private void doSkipImmutableValue(Object immutableValue) { HttpServletRequest request = mock(HttpServletRequest.class); HttpSession session = mock(HttpSession.class); - when(request.getSession(anyBoolean())).thenReturn(session); - when(session.getAttribute(KEY)).thenReturn(immutableValue); + given(request.getSession(anyBoolean())).willReturn(session); + given(session.getAttribute(KEY)).willReturn(immutableValue); ServletRequestAttributes attrs = new ServletRequestAttributes(request); attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyAdviceChainTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyAdviceChainTests.java index e18864dbdde..97f5b83381e 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyAdviceChainTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyAdviceChainTests.java @@ -16,6 +16,8 @@ package org.springframework.web.servlet.mvc.method.annotation; +import java.util.Arrays; + import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; @@ -35,10 +37,8 @@ import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.method.ControllerAdviceBean; -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.*; +import static org.junit.Assert.*; +import static org.mockito.BDDMockito.*; /** * Unit tests for @@ -80,9 +80,9 @@ public class ResponseBodyAdviceChainTests { ResponseBodyAdviceChain chain = new ResponseBodyAdviceChain(Arrays.asList(advice)); String expected = "body++"; - when(advice.supports(this.returnType, this.converterType)).thenReturn(true); - when(advice.beforeBodyWrite(eq(this.body), eq(this.returnType), eq(this.contentType), - eq(this.converterType), same(this.request), same(this.response))).thenReturn(expected); + given(advice.supports(this.returnType, this.converterType)).willReturn(true); + given(advice.beforeBodyWrite(eq(this.body), eq(this.returnType), eq(this.contentType), + eq(this.converterType), same(this.request), same(this.response))).willReturn(expected); String actual = chain.invoke(this.body, this.returnType, this.contentType, this.converterType, this.request, this.response); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/AppCacheManifestTransformerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/AppCacheManifestTransformerTests.java index 75210d071ac..8f97c19c3f9 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/AppCacheManifestTransformerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/AppCacheManifestTransformerTests.java @@ -16,9 +16,6 @@ package org.springframework.web.servlet.resource; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; - import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -28,11 +25,13 @@ import javax.servlet.http.HttpServletRequest; import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Test; - import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.util.FileCopyUtils; +import static org.junit.Assert.*; +import static org.mockito.BDDMockito.*; + /** * Unit tests for * {@link AppCacheManifestTransfomer}. @@ -57,8 +56,8 @@ public class AppCacheManifestTransformerTests { @Test public void noTransformIfExtensionNoMatch() throws Exception { Resource resource = mock(Resource.class); - when(resource.getFilename()).thenReturn("foobar.file"); - when(this.chain.transform(this.request, resource)).thenReturn(resource); + given(resource.getFilename()).willReturn("foobar.file"); + given(this.chain.transform(this.request, resource)).willReturn(resource); Resource result = this.transformer.transform(this.request, resource, this.chain); assertEquals(resource, result); @@ -67,7 +66,7 @@ public class AppCacheManifestTransformerTests { @Test public void syntaxErrorInManifest() throws Exception { Resource resource = new ClassPathResource("test/error.manifest", getClass()); - when(this.chain.transform(this.request, resource)).thenReturn(resource); + given(this.chain.transform(this.request, resource)).willReturn(resource); Resource result = this.transformer.transform(this.request, resource, this.chain); assertEquals(resource, result); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/VersionResourceResolverTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/VersionResourceResolverTests.java index 61d80000da0..f222f420f37 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/VersionResourceResolverTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/VersionResourceResolverTests.java @@ -23,12 +23,11 @@ import java.util.Map; import org.junit.Before; import org.junit.Test; - import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.mockito.BDDMockito.*; /** * Unit tests for {@link VersionResourceResolver} @@ -62,7 +61,7 @@ public class VersionResourceResolverTests { public void resolveResourceExisting() throws Exception { String file = "bar.css"; Resource expected = new ClassPathResource("test/" + file, getClass()); - when(this.chain.resolveResource(null, file, this.locations)).thenReturn(expected); + given(this.chain.resolveResource(null, file, this.locations)).willReturn(expected); this.resolver.setStrategyMap(Collections.singletonMap("/**", this.versionStrategy)); Resource actual = this.resolver.resolveResourceInternal(null, file, this.locations, this.chain); @@ -74,7 +73,7 @@ public class VersionResourceResolverTests { @Test public void resolveResourceNoVersionStrategy() throws Exception { String file = "missing.css"; - when(this.chain.resolveResource(null, file, this.locations)).thenReturn(null); + given(this.chain.resolveResource(null, file, this.locations)).willReturn(null); this.resolver.setStrategyMap(Collections.emptyMap()); Resource actual = this.resolver.resolveResourceInternal(null, file, this.locations, this.chain); @@ -85,8 +84,8 @@ public class VersionResourceResolverTests { @Test public void resolveResourceNoVersionInPath() throws Exception { String file = "bar.css"; - when(this.chain.resolveResource(null, file, this.locations)).thenReturn(null); - when(this.versionStrategy.extractVersion(file)).thenReturn(""); + given(this.chain.resolveResource(null, file, this.locations)).willReturn(null); + given(this.versionStrategy.extractVersion(file)).willReturn(""); this.resolver.setStrategyMap(Collections.singletonMap("/**", this.versionStrategy)); Resource actual = this.resolver.resolveResourceInternal(null, file, this.locations, this.chain); @@ -100,10 +99,10 @@ public class VersionResourceResolverTests { String versionFile = "bar-version.css"; String version = "version"; String file = "bar.css"; - when(this.chain.resolveResource(null, versionFile, this.locations)).thenReturn(null); - when(this.chain.resolveResource(null, file, this.locations)).thenReturn(null); - when(this.versionStrategy.extractVersion(versionFile)).thenReturn(version); - when(this.versionStrategy.removeVersion(versionFile, version)).thenReturn(file); + given(this.chain.resolveResource(null, versionFile, this.locations)).willReturn(null); + given(this.chain.resolveResource(null, file, this.locations)).willReturn(null); + given(this.versionStrategy.extractVersion(versionFile)).willReturn(version); + given(this.versionStrategy.removeVersion(versionFile, version)).willReturn(file); this.resolver.setStrategyMap(Collections.singletonMap("/**", this.versionStrategy)); Resource actual = this.resolver.resolveResourceInternal(null, versionFile, this.locations, this.chain); @@ -117,11 +116,11 @@ public class VersionResourceResolverTests { String version = "version"; String file = "bar.css"; Resource expected = new ClassPathResource("test/" + file, getClass()); - when(this.chain.resolveResource(null, versionFile, this.locations)).thenReturn(null); - when(this.chain.resolveResource(null, file, this.locations)).thenReturn(expected); - when(this.versionStrategy.extractVersion(versionFile)).thenReturn(version); - when(this.versionStrategy.removeVersion(versionFile, version)).thenReturn(file); - when(this.versionStrategy.getResourceVersion(expected)).thenReturn("newer-version"); + given(this.chain.resolveResource(null, versionFile, this.locations)).willReturn(null); + given(this.chain.resolveResource(null, file, this.locations)).willReturn(expected); + given(this.versionStrategy.extractVersion(versionFile)).willReturn(version); + given(this.versionStrategy.removeVersion(versionFile, version)).willReturn(file); + given(this.versionStrategy.getResourceVersion(expected)).willReturn("newer-version"); this.resolver.setStrategyMap(Collections.singletonMap("/**", this.versionStrategy)); Resource actual = this.resolver.resolveResourceInternal(null, versionFile, this.locations, this.chain); @@ -135,11 +134,11 @@ public class VersionResourceResolverTests { String version = "version"; String file = "bar.css"; Resource expected = new ClassPathResource("test/" + file, getClass()); - when(this.chain.resolveResource(null, versionFile, this.locations)).thenReturn(null); - when(this.chain.resolveResource(null, file, this.locations)).thenReturn(expected); - when(this.versionStrategy.extractVersion(versionFile)).thenReturn(version); - when(this.versionStrategy.removeVersion(versionFile, version)).thenReturn(file); - when(this.versionStrategy.getResourceVersion(expected)).thenReturn(version); + given(this.chain.resolveResource(null, versionFile, this.locations)).willReturn(null); + given(this.chain.resolveResource(null, file, this.locations)).willReturn(expected); + given(this.versionStrategy.extractVersion(versionFile)).willReturn(version); + given(this.versionStrategy.removeVersion(versionFile, version)).willReturn(file); + given(this.versionStrategy.getResourceVersion(expected)).willReturn(version); this.resolver .setStrategyMap(Collections.singletonMap("/**", this.versionStrategy)); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketHandlerAdapterTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketHandlerAdapterTests.java index d4d55e822ef..93a78d8bb47 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketHandlerAdapterTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketHandlerAdapterTests.java @@ -24,10 +24,8 @@ import org.junit.Test; import org.mockito.Mockito; import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.WebSocketHandler; -import org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter; -import org.springframework.web.socket.adapter.jetty.JettyWebSocketSession; -import static org.mockito.Mockito.*; +import static org.mockito.BDDMockito.*; /** * Test fixture for {@link org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter}. @@ -48,8 +46,8 @@ public class JettyWebSocketHandlerAdapterTests { @Before public void setup() { this.session = mock(Session.class); - when(this.session.getUpgradeRequest()).thenReturn(Mockito.mock(UpgradeRequest.class)); - when(this.session.getUpgradeResponse()).thenReturn(Mockito.mock(UpgradeResponse.class)); + given(this.session.getUpgradeRequest()).willReturn(Mockito.mock(UpgradeRequest.class)); + given(this.session.getUpgradeResponse()).willReturn(Mockito.mock(UpgradeResponse.class)); this.webSocketHandler = mock(WebSocketHandler.class); this.webSocketSession = new JettyWebSocketSession(null, null); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSessionTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSessionTests.java index aa525c61e97..727e48d38a7 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSessionTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSessionTests.java @@ -16,6 +16,9 @@ package org.springframework.web.socket.adapter.jetty; +import java.util.HashMap; +import java.util.Map; + import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.UpgradeRequest; import org.eclipse.jetty.websocket.api.UpgradeResponse; @@ -24,14 +27,8 @@ import org.junit.Test; import org.mockito.Mockito; import org.springframework.web.socket.handler.TestPrincipal; -import java.util.HashMap; -import java.util.Map; - -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.mockito.Mockito.reset; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; +import static org.junit.Assert.*; +import static org.mockito.BDDMockito.*; /** * Unit tests for {@link org.springframework.web.socket.adapter.jetty.JettyWebSocketSession}. @@ -63,14 +60,14 @@ public class JettyWebSocketSessionTests { TestPrincipal user = new TestPrincipal("joe"); UpgradeRequest request = Mockito.mock(UpgradeRequest.class); - when(request.getUserPrincipal()).thenReturn(user); + given(request.getUserPrincipal()).willReturn(user); UpgradeResponse response = Mockito.mock(UpgradeResponse.class); - when(response.getAcceptedSubProtocol()).thenReturn(null); + given(response.getAcceptedSubProtocol()).willReturn(null); Session nativeSession = Mockito.mock(Session.class); - when(nativeSession.getUpgradeRequest()).thenReturn(request); - when(nativeSession.getUpgradeResponse()).thenReturn(response); + given(nativeSession.getUpgradeRequest()).willReturn(request); + given(nativeSession.getUpgradeResponse()).willReturn(response); JettyWebSocketSession session = new JettyWebSocketSession(attributes); session.initializeNativeSession(nativeSession); @@ -85,14 +82,14 @@ public class JettyWebSocketSessionTests { public void getPrincipalNotAvailable() { UpgradeRequest request = Mockito.mock(UpgradeRequest.class); - when(request.getUserPrincipal()).thenReturn(null); + given(request.getUserPrincipal()).willReturn(null); UpgradeResponse response = Mockito.mock(UpgradeResponse.class); - when(response.getAcceptedSubProtocol()).thenReturn(null); + given(response.getAcceptedSubProtocol()).willReturn(null); Session nativeSession = Mockito.mock(Session.class); - when(nativeSession.getUpgradeRequest()).thenReturn(request); - when(nativeSession.getUpgradeResponse()).thenReturn(response); + given(nativeSession.getUpgradeRequest()).willReturn(request); + given(nativeSession.getUpgradeResponse()).willReturn(response); JettyWebSocketSession session = new JettyWebSocketSession(attributes); session.initializeNativeSession(nativeSession); @@ -109,14 +106,14 @@ public class JettyWebSocketSessionTests { String protocol = "foo"; UpgradeRequest request = Mockito.mock(UpgradeRequest.class); - when(request.getUserPrincipal()).thenReturn(null); + given(request.getUserPrincipal()).willReturn(null); UpgradeResponse response = Mockito.mock(UpgradeResponse.class); - when(response.getAcceptedSubProtocol()).thenReturn(protocol); + given(response.getAcceptedSubProtocol()).willReturn(protocol); Session nativeSession = Mockito.mock(Session.class); - when(nativeSession.getUpgradeRequest()).thenReturn(request); - when(nativeSession.getUpgradeResponse()).thenReturn(response); + given(nativeSession.getUpgradeRequest()).willReturn(request); + given(nativeSession.getUpgradeResponse()).willReturn(response); JettyWebSocketSession session = new JettyWebSocketSession(attributes); session.initializeNativeSession(nativeSession); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketHandlerAdapterTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketHandlerAdapterTests.java index 4919a8d5cd9..6f2fdb6debc 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketHandlerAdapterTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketHandlerAdapterTests.java @@ -27,7 +27,7 @@ import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.WebSocketHandler; import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.mockito.BDDMockito.*; /** * Test fixture for {@link org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter}. @@ -55,13 +55,13 @@ public class StandardWebSocketHandlerAdapterTests { @Test public void onOpen() throws Throwable { - when(this.session.getId()).thenReturn("123"); + given(this.session.getId()).willReturn("123"); this.adapter.onOpen(this.session, null); verify(this.webSocketHandler).afterConnectionEstablished(this.webSocketSession); verify(this.session, atLeast(2)).addMessageHandler(any(MessageHandler.Whole.class)); - when(this.session.getId()).thenReturn("123"); + given(this.session.getId()).willReturn("123"); assertEquals("123", this.webSocketSession.getId()); } diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSessionTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSessionTests.java index 9daa3c1c901..682fc0ba80c 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSessionTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSessionTests.java @@ -15,22 +15,19 @@ package org.springframework.web.socket.adapter.standard; +import java.util.HashMap; +import java.util.Map; + +import javax.websocket.Session; + import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import org.springframework.http.HttpHeaders; import org.springframework.web.socket.handler.TestPrincipal; -import javax.websocket.Session; -import java.util.HashMap; -import java.util.Map; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.mockito.Mockito.reset; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; +import static org.junit.Assert.*; +import static org.mockito.BDDMockito.*; /** * Unit tests for {@link org.springframework.web.socket.adapter.standard.StandardWebSocketSession}. @@ -65,7 +62,7 @@ public class StandardWebSocketSessionTests { TestPrincipal user = new TestPrincipal("joe"); Session nativeSession = Mockito.mock(Session.class); - when(nativeSession.getUserPrincipal()).thenReturn(user); + given(nativeSession.getUserPrincipal()).willReturn(user); StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null); session.initializeNativeSession(nativeSession); @@ -77,7 +74,7 @@ public class StandardWebSocketSessionTests { public void getPrincipalNone() { Session nativeSession = Mockito.mock(Session.class); - when(nativeSession.getUserPrincipal()).thenReturn(null); + given(nativeSession.getUserPrincipal()).willReturn(null); StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null); session.initializeNativeSession(nativeSession); @@ -94,7 +91,7 @@ public class StandardWebSocketSessionTests { String protocol = "foo"; Session nativeSession = Mockito.mock(Session.class); - when(nativeSession.getNegotiatedSubprotocol()).thenReturn(protocol); + given(nativeSession.getNegotiatedSubprotocol()).willReturn(protocol); StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null); session.initializeNativeSession(nativeSession); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/handler/ExceptionWebSocketHandlerDecoratorTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/handler/ExceptionWebSocketHandlerDecoratorTests.java index d1364c13046..c331f325755 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/handler/ExceptionWebSocketHandlerDecoratorTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/handler/ExceptionWebSocketHandlerDecoratorTests.java @@ -23,7 +23,7 @@ import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketHandler; import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.mockito.BDDMockito.*; /** * Test fixture for {@link ExceptionWebSocketHandlerDecorator}. @@ -52,8 +52,8 @@ public class ExceptionWebSocketHandlerDecoratorTests { @Test public void afterConnectionEstablished() throws Exception { - doThrow(new IllegalStateException("error")) - .when(this.delegate).afterConnectionEstablished(this.session); + willThrow(new IllegalStateException("error")) + .given(this.delegate).afterConnectionEstablished(this.session); this.decorator.afterConnectionEstablished(this.session); @@ -65,8 +65,8 @@ public class ExceptionWebSocketHandlerDecoratorTests { TextMessage message = new TextMessage("payload"); - doThrow(new IllegalStateException("error")) - .when(this.delegate).handleMessage(this.session, message); + willThrow(new IllegalStateException("error")) + .given(this.delegate).handleMessage(this.session, message); this.decorator.handleMessage(this.session, message); @@ -78,8 +78,8 @@ public class ExceptionWebSocketHandlerDecoratorTests { Exception exception = new Exception("transport error"); - doThrow(new IllegalStateException("error")) - .when(this.delegate).handleTransportError(this.session, exception); + willThrow(new IllegalStateException("error")) + .given(this.delegate).handleTransportError(this.session, exception); this.decorator.handleTransportError(this.session, exception); @@ -91,12 +91,12 @@ public class ExceptionWebSocketHandlerDecoratorTests { CloseStatus closeStatus = CloseStatus.NORMAL; - doThrow(new IllegalStateException("error")) - .when(this.delegate).afterConnectionClosed(this.session, closeStatus); + willThrow(new IllegalStateException("error")) + .given(this.delegate).afterConnectionClosed(this.session, closeStatus); this.decorator.afterConnectionClosed(this.session, closeStatus); assertNull(this.session.getCloseStatus()); } -} \ No newline at end of file +} diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandlerTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandlerTests.java index 66230c19cad..e6c0bc48972 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandlerTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandlerTests.java @@ -31,11 +31,8 @@ import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator; import org.springframework.web.socket.handler.TestWebSocketSession; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.*; +import static org.junit.Assert.*; +import static org.mockito.BDDMockito.*; /** * Test fixture for {@link SubProtocolWebSocketHandler}. @@ -65,8 +62,8 @@ public class SubProtocolWebSocketHandlerTests { public void setup() { MockitoAnnotations.initMocks(this); this.webSocketHandler = new SubProtocolWebSocketHandler(this.inClientChannel, this.outClientChannel); - when(stompHandler.getSupportedProtocols()).thenReturn(Arrays.asList("v10.stomp", "v11.stomp", "v12.stomp")); - when(mqttHandler.getSupportedProtocols()).thenReturn(Arrays.asList("MQTT")); + given(stompHandler.getSupportedProtocols()).willReturn(Arrays.asList("v10.stomp", "v11.stomp", "v12.stomp")); + given(mqttHandler.getSupportedProtocols()).willReturn(Arrays.asList("MQTT")); this.session = new TestWebSocketSession(); this.session.setId("1"); } diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/server/DefaultHandshakeHandlerTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/server/DefaultHandshakeHandlerTests.java index 5f04cdae280..2eb08785b01 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/server/DefaultHandshakeHandlerTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/server/DefaultHandshakeHandlerTests.java @@ -26,14 +26,14 @@ import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.web.socket.AbstractHttpRequestTests; -import org.springframework.web.socket.server.support.DefaultHandshakeHandler; import org.springframework.web.socket.SubProtocolCapable; import org.springframework.web.socket.WebSocketExtension; import org.springframework.web.socket.WebSocketHandler; -import org.springframework.web.socket.handler.TextWebSocketHandler; import org.springframework.web.socket.WebSocketHttpHeaders; +import org.springframework.web.socket.handler.TextWebSocketHandler; +import org.springframework.web.socket.server.support.DefaultHandshakeHandler; -import static org.mockito.Mockito.*; +import static org.mockito.BDDMockito.*; /** * Test fixture for {@link org.springframework.web.socket.server.support.DefaultHandshakeHandler}. @@ -60,7 +60,7 @@ public class DefaultHandshakeHandlerTests extends AbstractHttpRequestTests { this.handshakeHandler.setSupportedProtocols("stomp", "mqtt"); - when(this.upgradeStrategy.getSupportedVersions()).thenReturn(new String[] {"13"}); + given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"}); this.servletRequest.setMethod("GET"); @@ -86,8 +86,8 @@ public class DefaultHandshakeHandlerTests extends AbstractHttpRequestTests { WebSocketExtension extension1 = new WebSocketExtension("ext1"); WebSocketExtension extension2 = new WebSocketExtension("ext2"); - when(this.upgradeStrategy.getSupportedVersions()).thenReturn(new String[] {"13"}); - when(this.upgradeStrategy.getSupportedExtensions(this.request)).thenReturn(Arrays.asList(extension1)); + given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"}); + given(this.upgradeStrategy.getSupportedExtensions(this.request)).willReturn(Arrays.asList(extension1)); this.servletRequest.setMethod("GET"); @@ -109,7 +109,7 @@ public class DefaultHandshakeHandlerTests extends AbstractHttpRequestTests { @Test public void subProtocolCapableHandler() throws Exception { - when(this.upgradeStrategy.getSupportedVersions()).thenReturn(new String[]{"13"}); + given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[]{"13"}); this.servletRequest.setMethod("GET"); @@ -131,7 +131,7 @@ public class DefaultHandshakeHandlerTests extends AbstractHttpRequestTests { @Test public void subProtocolCapableHandlerNoMatch() throws Exception { - when(this.upgradeStrategy.getSupportedVersions()).thenReturn(new String[]{"13"}); + given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[]{"13"}); this.servletRequest.setMethod("GET"); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/server/support/HandshakeInterceptorChainTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/server/support/HandshakeInterceptorChainTests.java index 96aeb23dc5e..912b14bf32b 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/server/support/HandshakeInterceptorChainTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/server/support/HandshakeInterceptorChainTests.java @@ -27,7 +27,7 @@ import org.springframework.web.socket.AbstractHttpRequestTests; import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.server.HandshakeInterceptor; -import static org.mockito.Mockito.*; +import static org.mockito.BDDMockito.*; /** * Test fixture for {@link HandshakeInterceptorChain}. @@ -62,9 +62,9 @@ public class HandshakeInterceptorChainTests extends AbstractHttpRequestTests { @Test public void success() throws Exception { - when(i1.beforeHandshake(request, response, wsHandler, attributes)).thenReturn(true); - when(i2.beforeHandshake(request, response, wsHandler, attributes)).thenReturn(true); - when(i3.beforeHandshake(request, response, wsHandler, attributes)).thenReturn(true); + given(i1.beforeHandshake(request, response, wsHandler, attributes)).willReturn(true); + given(i2.beforeHandshake(request, response, wsHandler, attributes)).willReturn(true); + given(i3.beforeHandshake(request, response, wsHandler, attributes)).willReturn(true); HandshakeInterceptorChain chain = new HandshakeInterceptorChain(interceptors, wsHandler); chain.applyBeforeHandshake(request, response, attributes); @@ -77,8 +77,8 @@ public class HandshakeInterceptorChainTests extends AbstractHttpRequestTests { @Test public void applyBeforeHandshakeWithFalseReturnValue() throws Exception { - when(i1.beforeHandshake(request, response, wsHandler, attributes)).thenReturn(true); - when(i2.beforeHandshake(request, response, wsHandler, attributes)).thenReturn(false); + given(i1.beforeHandshake(request, response, wsHandler, attributes)).willReturn(true); + given(i2.beforeHandshake(request, response, wsHandler, attributes)).willReturn(false); HandshakeInterceptorChain chain = new HandshakeInterceptorChain(interceptors, wsHandler); chain.applyBeforeHandshake(request, response, attributes); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/ClientSockJsSessionTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/ClientSockJsSessionTests.java index 9d3f76b4600..6fb97fb7dbb 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/ClientSockJsSessionTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/ClientSockJsSessionTests.java @@ -16,6 +16,11 @@ package org.springframework.web.socket.sockjs.client; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.URI; +import java.util.List; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -30,15 +35,9 @@ import org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec; import org.springframework.web.socket.sockjs.frame.SockJsFrame; import org.springframework.web.socket.sockjs.transport.TransportType; -import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.URI; -import java.util.List; - -import static org.junit.Assert.assertThat; import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.*; +import static org.junit.Assert.*; +import static org.mockito.BDDMockito.*; /** * Unit tests for @@ -47,7 +46,7 @@ import static org.mockito.Mockito.*; * @author Rossen Stoyanchev */ public class ClientSockJsSessionTests { - + private static final Jackson2SockJsMessageCodec CODEC = new Jackson2SockJsMessageCodec(); private TestClientSockJsSession session; @@ -92,7 +91,7 @@ public class ClientSockJsSessionTests { @Test public void handleFrameOpenWithWebSocketHandlerException() throws Exception { - doThrow(new IllegalStateException("Fake error")).when(this.handler).afterConnectionEstablished(this.session); + willThrow(new IllegalStateException("Fake error")).given(this.handler).afterConnectionEstablished(this.session); this.session.handleFrame(SockJsFrame.openFrame().getContent()); assertThat(this.session.isOpen(), is(true)); } @@ -129,8 +128,8 @@ public class ClientSockJsSessionTests { @Test public void handleFrameMessageWithWebSocketHandlerException() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); - doThrow(new IllegalStateException("Fake error")).when(this.handler).handleMessage(this.session, new TextMessage("foo")); - doThrow(new IllegalStateException("Fake error")).when(this.handler).handleMessage(this.session, new TextMessage("bar")); + willThrow(new IllegalStateException("Fake error")).given(this.handler).handleMessage(this.session, new TextMessage("foo")); + willThrow(new IllegalStateException("Fake error")).given(this.handler).handleMessage(this.session, new TextMessage("bar")); this.session.handleFrame(SockJsFrame.messageFrame(CODEC, "foo", "bar").getContent()); assertThat(this.session.isOpen(), equalTo(true)); verify(this.handler).afterConnectionEstablished(this.session); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/RestTemplateXhrTransportTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/RestTemplateXhrTransportTests.java index 4fcca2d16ca..42f6be12e7e 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/RestTemplateXhrTransportTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/RestTemplateXhrTransportTests.java @@ -16,6 +16,16 @@ package org.springframework.web.socket.sockjs.client; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.nio.charset.Charset; +import java.util.Arrays; +import java.util.Queue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.LinkedBlockingDeque; + import org.junit.Before; import org.junit.Test; import org.springframework.core.task.SyncTaskExecutor; @@ -45,17 +55,7 @@ import org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec; import org.springframework.web.socket.sockjs.frame.SockJsFrame; import org.springframework.web.socket.sockjs.transport.TransportType; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.nio.charset.Charset; -import java.util.Arrays; -import java.util.Queue; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.LinkedBlockingDeque; - -import static org.mockito.Mockito.*; +import static org.mockito.BDDMockito.*; /** * Unit tests for {@link RestTemplateXhrTransport}. @@ -127,7 +127,7 @@ public class RestTemplateXhrTransportTests { public void connectFailure() throws Exception { final HttpServerErrorException expected = new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR); RestOperations restTemplate = mock(RestOperations.class); - when(restTemplate.execute(any(), eq(HttpMethod.POST), any(), any())).thenThrow(expected); + given(restTemplate.execute((URI) any(), eq(HttpMethod.POST), any(), any())).willThrow(expected); final CountDownLatch latch = new CountDownLatch(1); connect(restTemplate).addCallback( @@ -189,8 +189,8 @@ public class RestTemplateXhrTransportTests { private ClientHttpResponse response(HttpStatus status, String body) throws IOException { ClientHttpResponse response = mock(ClientHttpResponse.class); InputStream inputStream = getInputStream(body); - when(response.getStatusCode()).thenReturn(status); - when(response.getBody()).thenReturn(inputStream); + given(response.getStatusCode()).willReturn(status); + given(response.getBody()).willReturn(inputStream); return response; } diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/SockJsClientTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/SockJsClientTests.java index b3042578035..21933e9af67 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/SockJsClientTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/SockJsClientTests.java @@ -16,6 +16,10 @@ package org.springframework.web.socket.sockjs.client; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; + import org.junit.Before; import org.junit.Test; import org.springframework.http.HttpStatus; @@ -25,13 +29,8 @@ import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.sockjs.client.TestTransport.XhrTestTransport; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.List; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.*; +import static org.junit.Assert.*; +import static org.mockito.BDDMockito.*; /** * Unit tests for {@link org.springframework.web.socket.sockjs.client.SockJsClient}. @@ -41,7 +40,7 @@ import static org.mockito.Mockito.*; public class SockJsClientTests { private static final String URL = "http://example.com"; - + private static final WebSocketHandler handler = mock(WebSocketHandler.class); @@ -122,7 +121,7 @@ public class SockJsClientTests { @SuppressWarnings("unchecked") public void connectInfoRequestFailure() throws URISyntaxException { HttpServerErrorException exception = new HttpServerErrorException(HttpStatus.SERVICE_UNAVAILABLE); - when(this.infoReceiver.executeInfoRequest(any())).thenThrow(exception); + given(this.infoReceiver.executeInfoRequest(any())).willThrow(exception); this.sockJsClient.doHandshake(handler, URL).addCallback(this.connectCallback); verify(this.connectCallback).onFailure(exception); assertFalse(this.webSocketTransport.invoked()); @@ -130,7 +129,7 @@ public class SockJsClientTests { } private void setupInfoRequest(boolean webSocketEnabled) { - when(this.infoReceiver.executeInfoRequest(any())).thenReturn("{\"entropy\":123," + + given(this.infoReceiver.executeInfoRequest(any())).willReturn("{\"entropy\":123," + "\"origins\":[\"*:*\"],\"cookie_needed\":true,\"websocket\":" + webSocketEnabled + "}"); } diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/XhrTransportTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/XhrTransportTests.java index fee2b4e0d84..ea78728d2a1 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/XhrTransportTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/XhrTransportTests.java @@ -16,6 +16,8 @@ package org.springframework.web.socket.sockjs.client; +import java.net.URI; + import org.junit.Test; import org.mockito.ArgumentCaptor; import org.springframework.http.HttpHeaders; @@ -28,15 +30,8 @@ import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.WebSocketSession; -import java.net.URI; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; +import static org.junit.Assert.*; +import static org.mockito.BDDMockito.*; /** * Unit tests for @@ -88,8 +83,8 @@ public class XhrTransportTests { handshakeHeaders.setOrigin("foo"); TransportRequest request = mock(TransportRequest.class); - when(request.getSockJsUrlInfo()).thenReturn(new SockJsUrlInfo(new URI("http://example.com"))); - when(request.getHandshakeHeaders()).thenReturn(handshakeHeaders); + given(request.getSockJsUrlInfo()).willReturn(new SockJsUrlInfo(new URI("http://example.com"))); + given(request.getHandshakeHeaders()).willReturn(handshakeHeaders); HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("foo", "bar"); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/support/SockJsServiceTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/support/SockJsServiceTests.java index 36c8d64e2df..d2bae76be38 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/support/SockJsServiceTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/support/SockJsServiceTests.java @@ -18,6 +18,9 @@ package org.springframework.web.socket.sockjs.support; import java.io.IOException; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; + import org.junit.Before; import org.junit.Test; import org.springframework.http.HttpStatus; @@ -31,10 +34,7 @@ import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.sockjs.SockJsException; import static org.junit.Assert.*; -import static org.mockito.Mockito.*; - -import javax.servlet.ServletOutputStream; -import javax.servlet.http.HttpServletResponse; +import static org.mockito.BDDMockito.*; /** * Test fixture for {@link AbstractSockJsService}. @@ -117,8 +117,8 @@ public class SockJsServiceTests extends AbstractHttpRequestTests { public void handleInfoGetWildflyNPE() throws Exception { HttpServletResponse mockResponse = mock(HttpServletResponse.class); ServletOutputStream ous = mock(ServletOutputStream.class); - when(mockResponse.getHeaders("Access-Control-Allow-Origin")).thenThrow(NullPointerException.class); - when(mockResponse.getOutputStream()).thenReturn(ous); + given(mockResponse.getHeaders("Access-Control-Allow-Origin")).willThrow(NullPointerException.class); + given(mockResponse.getOutputStream()).willReturn(ous); this.response = new ServletServerHttpResponse(mockResponse); handleRequest("GET", "/echo/info", HttpStatus.OK); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/DefaultSockJsServiceTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/DefaultSockJsServiceTests.java index ac85ee8f4de..86d1e386d3a 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/DefaultSockJsServiceTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/DefaultSockJsServiceTests.java @@ -23,7 +23,6 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; - import org.springframework.scheduling.TaskScheduler; import org.springframework.web.socket.AbstractHttpRequestTests; import org.springframework.web.socket.WebSocketHandler; @@ -35,7 +34,7 @@ import org.springframework.web.socket.sockjs.transport.session.StubSockJsService import org.springframework.web.socket.sockjs.transport.session.TestSockJsSession; import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.mockito.BDDMockito.*; /** * Test fixture for {@link org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService}. @@ -72,9 +71,9 @@ public class DefaultSockJsServiceTests extends AbstractHttpRequestTests { Map attributes = Collections.emptyMap(); this.session = new TestSockJsSession(sessionId, new StubSockJsServiceConfig(), this.wsHandler, attributes); - when(this.xhrHandler.getTransportType()).thenReturn(TransportType.XHR); - when(this.xhrHandler.createSession(sessionId, this.wsHandler, attributes)).thenReturn(this.session); - when(this.xhrSendHandler.getTransportType()).thenReturn(TransportType.XHR_SEND); + given(this.xhrHandler.getTransportType()).willReturn(TransportType.XHR); + given(this.xhrHandler.createSession(sessionId, this.wsHandler, attributes)).willReturn(this.session); + given(this.xhrSendHandler.getTransportType()).willReturn(TransportType.XHR_SEND); this.service = new TransportHandlingSockJsService(this.taskScheduler, this.xhrHandler, this.xhrSendHandler); } diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/HttpReceivingTransportHandlerTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/HttpReceivingTransportHandlerTests.java index 846a2bbd6c1..0ae63df5d2e 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/HttpReceivingTransportHandlerTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/HttpReceivingTransportHandlerTests.java @@ -28,7 +28,7 @@ import org.springframework.web.socket.sockjs.transport.session.StubSockJsService import org.springframework.web.socket.sockjs.transport.session.TestHttpSockJsSession; import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.mockito.BDDMockito.*; /** * Test fixture for {@link AbstractHttpReceivingTransportHandler} and sub-classes @@ -110,7 +110,7 @@ public class HttpReceivingTransportHandlerTests extends AbstractHttpRequestTest TestHttpSockJsSession session = new TestHttpSockJsSession("1", sockJsConfig, wsHandler, null); session.delegateConnectionEstablished(); - doThrow(new Exception()).when(wsHandler).handleMessage(session, new TextMessage("x")); + willThrow(new Exception()).given(wsHandler).handleMessage(session, new TextMessage("x")); try { XhrReceivingTransportHandler transportHandler = new XhrReceivingTransportHandler(); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/session/SockJsSessionTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/session/SockJsSessionTests.java index 9f01c4bd2f6..5f25f10e081 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/session/SockJsSessionTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/session/SockJsSessionTests.java @@ -26,13 +26,13 @@ import org.junit.Test; import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator; import org.springframework.web.socket.sockjs.SockJsMessageDeliveryException; import org.springframework.web.socket.sockjs.SockJsTransportFailureException; import org.springframework.web.socket.sockjs.frame.SockJsFrame; -import org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator; import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.mockito.BDDMockito.*; /** * Test fixture for {@link AbstractSockJsSession}. @@ -108,7 +108,7 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests task = mock(ScheduledFuture.class); - doReturn(task).when(this.taskScheduler).schedule(any(Runnable.class), any(Date.class)); + willReturn(task).given(this.taskScheduler).schedule(any(Runnable.class), any(Date.class)); this.session.setActive(true); this.session.scheduleHeartbeat(); @@ -286,7 +286,7 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests