From ac774cdcef7b25878f075d7e3514c373880eacac Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 4 Nov 2016 12:24:46 +0100 Subject: [PATCH] Avoid deprecated Mockito methods Issue: SPR-14880 --- .../annotation/ImportSelectorTests.java | 8 ++-- .../core/ResolvableTypeTests.java | 2 +- .../jms/core/JmsMessagingTemplateTests.java | 46 +++++++++---------- .../simp/stomp/DefaultStompSessionTests.java | 2 +- .../ProtobufHttpMessageConverterTests.java | 2 +- .../ModelAttributeMethodProcessorTests.java | 4 +- .../messaging/WebSocketStompClientTests.java | 23 +++++----- 7 files changed, 44 insertions(+), 43 deletions(-) diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ImportSelectorTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ImportSelectorTests.java index c274f12409..e877917d8f 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ImportSelectorTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ImportSelectorTests.java @@ -71,10 +71,10 @@ public class ImportSelectorTests { context.refresh(); context.getBean(Config.class); InOrder ordered = inOrder(beanFactory); - ordered.verify(beanFactory).registerBeanDefinition(eq("a"), (BeanDefinition) anyObject()); - ordered.verify(beanFactory).registerBeanDefinition(eq("b"), (BeanDefinition) anyObject()); - ordered.verify(beanFactory).registerBeanDefinition(eq("d"), (BeanDefinition) anyObject()); - ordered.verify(beanFactory).registerBeanDefinition(eq("c"), (BeanDefinition) anyObject()); + ordered.verify(beanFactory).registerBeanDefinition(eq("a"), any()); + ordered.verify(beanFactory).registerBeanDefinition(eq("b"), any()); + ordered.verify(beanFactory).registerBeanDefinition(eq("d"), any()); + ordered.verify(beanFactory).registerBeanDefinition(eq("c"), any()); } @Test diff --git a/spring-core/src/test/java/org/springframework/core/ResolvableTypeTests.java b/spring-core/src/test/java/org/springframework/core/ResolvableTypeTests.java index 6865b47b30..2ba78ad4ad 100644 --- a/spring-core/src/test/java/org/springframework/core/ResolvableTypeTests.java +++ b/spring-core/src/test/java/org/springframework/core/ResolvableTypeTests.java @@ -855,7 +855,7 @@ public class ResolvableTypeTests { VariableResolver variableResolver = mock(VariableResolver.class); given(variableResolver.getSource()).willReturn(this); ResolvableType longType = ResolvableType.forClass(Long.class); - given(variableResolver.resolveVariable((TypeVariable) anyObject())).willReturn(longType); + given(variableResolver.resolveVariable(any())).willReturn(longType); ResolvableType variable = ResolvableType.forType( Fields.class.getField("typeVariableType").getGenericType(), variableResolver); 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 19bcaacd81..3bd7bf61bc 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 @@ -353,10 +353,10 @@ public class JmsMessagingTemplateTests { Destination destination = new Destination() {}; Message request = createTextMessage(); javax.jms.Message replyJmsMessage = createJmsTextMessage(); - given(jmsTemplate.sendAndReceive(eq(destination), anyObject())).willReturn(replyJmsMessage); + given(jmsTemplate.sendAndReceive(eq(destination), any())).willReturn(replyJmsMessage); Message actual = messagingTemplate.sendAndReceive(destination, request); - verify(jmsTemplate, times(1)).sendAndReceive(eq(destination), anyObject()); + verify(jmsTemplate, times(1)).sendAndReceive(eq(destination), any()); assertTextMessage(actual); } @@ -364,10 +364,10 @@ public class JmsMessagingTemplateTests { public void sendAndReceiveName() { Message request = createTextMessage(); javax.jms.Message replyJmsMessage = createJmsTextMessage(); - given(jmsTemplate.sendAndReceive(eq("myQueue"), anyObject())).willReturn(replyJmsMessage); + given(jmsTemplate.sendAndReceive(eq("myQueue"), any())).willReturn(replyJmsMessage); Message actual = messagingTemplate.sendAndReceive("myQueue", request); - verify(jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), anyObject()); + verify(jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), any()); assertTextMessage(actual); } @@ -377,10 +377,10 @@ public class JmsMessagingTemplateTests { messagingTemplate.setDefaultDestination(destination); Message request = createTextMessage(); javax.jms.Message replyJmsMessage = createJmsTextMessage(); - given(jmsTemplate.sendAndReceive(eq(destination), anyObject())).willReturn(replyJmsMessage); + given(jmsTemplate.sendAndReceive(eq(destination), any())).willReturn(replyJmsMessage); Message actual = messagingTemplate.sendAndReceive(request); - verify(jmsTemplate, times(1)).sendAndReceive(eq(destination), anyObject()); + verify(jmsTemplate, times(1)).sendAndReceive(eq(destination), any()); assertTextMessage(actual); } @@ -389,10 +389,10 @@ public class JmsMessagingTemplateTests { messagingTemplate.setDefaultDestinationName("myQueue"); Message request = createTextMessage(); javax.jms.Message replyJmsMessage = createJmsTextMessage(); - given(jmsTemplate.sendAndReceive(eq("myQueue"), anyObject())).willReturn(replyJmsMessage); + given(jmsTemplate.sendAndReceive(eq("myQueue"), any())).willReturn(replyJmsMessage); Message actual = messagingTemplate.sendAndReceive(request); - verify(jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), anyObject()); + verify(jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), any()); assertTextMessage(actual); } @@ -408,20 +408,20 @@ public class JmsMessagingTemplateTests { public void convertSendAndReceivePayload() throws JMSException { Destination destination = new Destination() {}; javax.jms.Message replyJmsMessage = createJmsTextMessage("My reply"); - given(jmsTemplate.sendAndReceive(eq(destination), anyObject())).willReturn(replyJmsMessage); + given(jmsTemplate.sendAndReceive(eq(destination), any())).willReturn(replyJmsMessage); String reply = messagingTemplate.convertSendAndReceive(destination, "my Payload", String.class); - verify(jmsTemplate, times(1)).sendAndReceive(eq(destination), anyObject()); + verify(jmsTemplate, times(1)).sendAndReceive(eq(destination), any()); assertEquals("My reply", reply); } @Test public void convertSendAndReceivePayloadName() throws JMSException { javax.jms.Message replyJmsMessage = createJmsTextMessage("My reply"); - given(jmsTemplate.sendAndReceive(eq("myQueue"), anyObject())).willReturn(replyJmsMessage); + given(jmsTemplate.sendAndReceive(eq("myQueue"), any())).willReturn(replyJmsMessage); String reply = messagingTemplate.convertSendAndReceive("myQueue", "my Payload", String.class); - verify(jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), anyObject()); + verify(jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), any()); assertEquals("My reply", reply); } @@ -430,10 +430,10 @@ public class JmsMessagingTemplateTests { Destination destination = new Destination() {}; messagingTemplate.setDefaultDestination(destination); javax.jms.Message replyJmsMessage = createJmsTextMessage("My reply"); - given(jmsTemplate.sendAndReceive(eq(destination), anyObject())).willReturn(replyJmsMessage); + given(jmsTemplate.sendAndReceive(eq(destination), any())).willReturn(replyJmsMessage); String reply = messagingTemplate.convertSendAndReceive("my Payload", String.class); - verify(jmsTemplate, times(1)).sendAndReceive(eq(destination), anyObject()); + verify(jmsTemplate, times(1)).sendAndReceive(eq(destination), any()); assertEquals("My reply", reply); } @@ -441,10 +441,10 @@ public class JmsMessagingTemplateTests { public void convertSendAndReceiveDefaultDestinationName() throws JMSException { messagingTemplate.setDefaultDestinationName("myQueue"); javax.jms.Message replyJmsMessage = createJmsTextMessage("My reply"); - given(jmsTemplate.sendAndReceive(eq("myQueue"), anyObject())).willReturn(replyJmsMessage); + given(jmsTemplate.sendAndReceive(eq("myQueue"), any())).willReturn(replyJmsMessage); String reply = messagingTemplate.convertSendAndReceive("my Payload", String.class); - verify(jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), anyObject()); + verify(jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), any()); assertEquals("My reply", reply); } @@ -459,7 +459,7 @@ public class JmsMessagingTemplateTests { Message message = createTextMessage(); MessageConverter messageConverter = mock(MessageConverter.class); willThrow(org.springframework.jms.support.converter.MessageConversionException.class) - .given(messageConverter).toMessage(eq(message), anyObject()); + .given(messageConverter).toMessage(eq(message), any()); messagingTemplate.setJmsMessageConverter(messageConverter); invokeMessageCreator("myQueue"); @@ -491,7 +491,7 @@ public class JmsMessagingTemplateTests { @Test public void convertDestinationResolutionExceptionOnSend() { Destination destination = new Destination() {}; - willThrow(DestinationResolutionException.class).given(jmsTemplate).send(eq(destination), anyObject()); + willThrow(DestinationResolutionException.class).given(jmsTemplate).send(eq(destination), any()); thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class); messagingTemplate.send(destination, createTextMessage()); @@ -510,7 +510,7 @@ public class JmsMessagingTemplateTests { public void convertMessageFormatException() throws JMSException { Message message = createTextMessage(); MessageConverter messageConverter = mock(MessageConverter.class); - willThrow(MessageFormatException.class).given(messageConverter).toMessage(eq(message), anyObject()); + willThrow(MessageFormatException.class).given(messageConverter).toMessage(eq(message), any()); 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); - willThrow(MessageNotWriteableException.class).given(messageConverter).toMessage(eq(message), anyObject()); + willThrow(MessageNotWriteableException.class).given(messageConverter).toMessage(eq(message), any()); messagingTemplate.setJmsMessageConverter(messageConverter); invokeMessageCreator("myQueue"); @@ -532,7 +532,7 @@ public class JmsMessagingTemplateTests { @Test public void convertInvalidDestinationExceptionOnSendAndReceiveWithName() { - willThrow(InvalidDestinationException.class).given(jmsTemplate).sendAndReceive(eq("unknownQueue"), anyObject()); + willThrow(InvalidDestinationException.class).given(jmsTemplate).sendAndReceive(eq("unknownQueue"), any()); thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class); messagingTemplate.sendAndReceive("unknownQueue", createTextMessage()); @@ -541,7 +541,7 @@ public class JmsMessagingTemplateTests { @Test public void convertInvalidDestinationExceptionOnSendAndReceive() { Destination destination = new Destination() {}; - willThrow(InvalidDestinationException.class).given(jmsTemplate).sendAndReceive(eq(destination), anyObject()); + willThrow(InvalidDestinationException.class).given(jmsTemplate).sendAndReceive(eq(destination), any()); thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class); messagingTemplate.sendAndReceive(destination, createTextMessage()); @@ -555,7 +555,7 @@ public class JmsMessagingTemplateTests { messageCreator.createMessage(null); return null; } - }).given(jmsTemplate).send(eq("myQueue"), anyObject()); + }).given(jmsTemplate).send(eq("myQueue"), any()); } diff --git a/spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/DefaultStompSessionTests.java b/spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/DefaultStompSessionTests.java index fdccab39e0..5744ac1ce2 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/DefaultStompSessionTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/DefaultStompSessionTests.java @@ -572,7 +572,7 @@ public class DefaultStompSessionTests { receiptable.addReceiptLostTask(() -> notReceived.set(true)); ArgumentCaptor taskCaptor = ArgumentCaptor.forClass(Runnable.class); - verify(taskScheduler).schedule(taskCaptor.capture(), notNull(Date.class)); + verify(taskScheduler).schedule(taskCaptor.capture(), (Date) notNull()); Runnable scheduledTask = taskCaptor.getValue(); assertNotNull(scheduledTask); diff --git a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java index 0a82b7d469..1ef5b55752 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java @@ -55,7 +55,7 @@ public class ProtobufHttpMessageConverterTests { @Test public void extensionRegistryInitialized() { - verify(this.registryInitializer, times(1)).initializeExtensionRegistry(anyObject()); + verify(this.registryInitializer, times(1)).initializeExtensionRegistry(any()); } @Test diff --git a/spring-web/src/test/java/org/springframework/web/method/annotation/ModelAttributeMethodProcessorTests.java b/spring-web/src/test/java/org/springframework/web/method/annotation/ModelAttributeMethodProcessorTests.java index 2010ba9dd9..8d0fb33361 100644 --- a/spring-web/src/test/java/org/springframework/web/method/annotation/ModelAttributeMethodProcessorTests.java +++ b/spring-web/src/test/java/org/springframework/web/method/annotation/ModelAttributeMethodProcessorTests.java @@ -148,10 +148,10 @@ public class ModelAttributeMethodProcessorTests { public void resovleArgumentViaDefaultConstructor() throws Exception { WebDataBinder dataBinder = new WebRequestDataBinder(null); WebDataBinderFactory factory = mock(WebDataBinderFactory.class); - given(factory.createBinder(anyObject(), notNull(), eq("attrName"))).willReturn(dataBinder); + given(factory.createBinder(any(), notNull(), eq("attrName"))).willReturn(dataBinder); this.processor.resolveArgument(this.paramNamedValidModelAttr, this.container, this.request, factory); - verify(factory).createBinder(anyObject(), notNull(), eq("attrName")); + verify(factory).createBinder(any(), notNull(), eq("attrName")); } @Test diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/messaging/WebSocketStompClientTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/messaging/WebSocketStompClientTests.java index cd5c5ef081..601c058960 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/messaging/WebSocketStompClientTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/messaging/WebSocketStompClientTests.java @@ -56,21 +56,22 @@ import static org.mockito.Mockito.*; */ public class WebSocketStompClientTests { - private TestWebSocketStompClient stompClient; - @Mock private TaskScheduler taskScheduler; @Mock private ConnectionHandlingStompSession stompSession; + @Mock + private WebSocketSession webSocketSession; + + + private TestWebSocketStompClient stompClient; + private ArgumentCaptor webSocketHandlerCaptor; private SettableListenableFuture handshakeFuture; - @Mock - private WebSocketSession webSocketSession; - @Before public void setUp() throws Exception { @@ -102,7 +103,7 @@ public class WebSocketStompClientTests { @SuppressWarnings("unchecked") public void webSocketConnectionEstablished() throws Exception { connect().afterConnectionEstablished(this.webSocketSession); - verify(this.stompSession).afterConnected(isNotNull(TcpConnection.class)); + verify(this.stompSession).afterConnected(notNull()); } @Test @@ -120,7 +121,7 @@ public class WebSocketStompClientTests { } @Test - @SuppressWarnings({ "unchecked", "rawtypes" }) + @SuppressWarnings({"unchecked", "rawtypes"}) public void handleWebSocketMessage() throws Exception { String text = "SEND\na:alpha\n\nMessage payload\0"; connect().handleMessage(this.webSocketSession, new TextMessage(text)); @@ -138,7 +139,7 @@ public class WebSocketStompClientTests { } @Test - @SuppressWarnings({ "unchecked", "rawtypes" }) + @SuppressWarnings({"unchecked", "rawtypes"}) public void handleWebSocketMessageSplitAcrossTwoMessage() throws Exception { WebSocketHandler webSocketHandler = connect(); @@ -163,7 +164,7 @@ public class WebSocketStompClientTests { } @Test - @SuppressWarnings({ "unchecked", "rawtypes" }) + @SuppressWarnings({"unchecked", "rawtypes"}) public void handleWebSocketMessageBinary() throws Exception { String text = "SEND\na:alpha\n\nMessage payload\0"; connect().handleMessage(this.webSocketSession, new BinaryMessage(text.getBytes(StandardCharsets.UTF_8))); @@ -246,7 +247,7 @@ public class WebSocketStompClientTests { fail("Expected IllegalStateException"); } catch (IllegalStateException ex) { - // Ignore + // ignore } } @@ -287,7 +288,7 @@ public class WebSocketStompClientTests { } @Test - @SuppressWarnings({ "rawtypes", "unchecked" }) + @SuppressWarnings({"rawtypes", "unchecked"}) public void cancelInactivityTasks() throws Exception { TcpConnection tcpConnection = getTcpConnection();