From 73fdff5a3fd4e875d61d24d01c2097e62c0d6e33 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Tue, 21 Jul 2020 17:22:46 -0400 Subject: [PATCH] Consistent Use of BDDMockito Mixture of plain and BDD Mockito, sometimes in the same class. Enforce with checkstyle. --- .../rabbit/test/RabbitListenerProxyTest.java | 4 +- ...xampleRabbitListenerSpyAndCaptureTest.java | 4 +- .../ExampleRabbitListenerSpyTest.java | 4 +- .../EnableRabbitIntegrationTests.java | 6 +- .../AbstractConnectionFactoryTests.java | 28 +- .../CachingConnectionFactoryTests.java | 320 +++++++++--------- .../ClientRecoveryCompatibilityTests.java | 20 +- .../LocalizedQueueConnectionFactoryTests.java | 28 +- .../SingleConnectionFactoryTests.java | 10 +- .../core/BatchingRabbitTemplateTests.java | 8 +- .../core/RabbitAdminDeclarationTests.java | 88 +++-- .../amqp/rabbit/core/RabbitAdminTests.java | 5 +- .../core/RabbitTemplateHeaderTests.java | 56 +-- .../core/RabbitTemplateIntegrationTests.java | 28 +- ...atePublisherCallbacksIntegrationTests.java | 108 +++--- .../listener/BlockingQueueConsumerTests.java | 62 ++-- .../ListenFromAutoDeleteQueueTests.java | 10 +- ...ContainerErrorHandlerIntegrationTests.java | 12 +- ...nerContainerLifecycleIntegrationTests.java | 10 +- ...ageListenerContainerIntegration2Tests.java | 16 +- .../SimpleMessageListenerContainerTests.java | 119 ++++--- src/checkstyle/checkstyle.xml | 5 + 22 files changed, 474 insertions(+), 477 deletions(-) diff --git a/spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/RabbitListenerProxyTest.java b/spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/RabbitListenerProxyTest.java index 18af387f..20395ced 100644 --- a/spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/RabbitListenerProxyTest.java +++ b/spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/RabbitListenerProxyTest.java @@ -18,7 +18,7 @@ package org.springframework.amqp.rabbit.test; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.doAnswer; +import static org.mockito.BDDMockito.willAnswer; import static org.mockito.Mockito.verify; import org.junit.jupiter.api.Test; @@ -67,7 +67,7 @@ public class RabbitListenerProxyTest { assertThat(listener).isNotNull(); LatchCountDownAndCallRealMethodAnswer answer = this.harness.getLatchAnswerFor("foo", 1); - doAnswer(answer).when(listener).foo(anyString()); + willAnswer(answer).given(listener).foo(anyString()); this.rabbitTemplate.convertAndSend(this.queue.getName(), "foo"); diff --git a/spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/examples/ExampleRabbitListenerSpyAndCaptureTest.java b/spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/examples/ExampleRabbitListenerSpyAndCaptureTest.java index 098a6445..03349f9e 100644 --- a/spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/examples/ExampleRabbitListenerSpyAndCaptureTest.java +++ b/spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/examples/ExampleRabbitListenerSpyAndCaptureTest.java @@ -18,7 +18,7 @@ package org.springframework.amqp.rabbit.test.examples; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.doAnswer; +import static org.mockito.BDDMockito.willAnswer; import static org.mockito.Mockito.verify; import java.util.Collection; @@ -89,7 +89,7 @@ public class ExampleRabbitListenerSpyAndCaptureTest { assertThat(listener).isNotNull(); LatchCountDownAndCallRealMethodAnswer answer = this.harness.getLatchAnswerFor("bar", 3); - doAnswer(answer).when(listener).foo(anyString(), anyString()); + willAnswer(answer).given(listener).foo(anyString(), anyString()); this.rabbitTemplate.convertAndSend(this.queue2.getName(), "bar"); this.rabbitTemplate.convertAndSend(this.queue2.getName(), "baz"); diff --git a/spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/examples/ExampleRabbitListenerSpyTest.java b/spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/examples/ExampleRabbitListenerSpyTest.java index 46128180..45cddf63 100644 --- a/spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/examples/ExampleRabbitListenerSpyTest.java +++ b/spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/examples/ExampleRabbitListenerSpyTest.java @@ -18,7 +18,7 @@ package org.springframework.amqp.rabbit.test.examples; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.doAnswer; +import static org.mockito.BDDMockito.willAnswer; import static org.mockito.Mockito.verify; import org.junit.jupiter.api.Test; @@ -83,7 +83,7 @@ public class ExampleRabbitListenerSpyTest { assertThat(listener).isNotNull(); LatchCountDownAndCallRealMethodAnswer answer = this.harness.getLatchAnswerFor("bar", 2); - doAnswer(answer).when(listener).foo(anyString(), anyString()); + willAnswer(answer).given(listener).foo(anyString(), anyString()); this.rabbitTemplate.convertAndSend(this.queue2.getName(), "bar"); this.rabbitTemplate.convertAndSend(this.queue2.getName(), "baz"); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/annotation/EnableRabbitIntegrationTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/annotation/EnableRabbitIntegrationTests.java index 72e6c35a..3ed5d4bc 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/annotation/EnableRabbitIntegrationTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/annotation/EnableRabbitIntegrationTests.java @@ -19,7 +19,7 @@ package org.springframework.amqp.rabbit.annotation; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; import static org.awaitility.Awaitility.await; -import static org.mockito.Mockito.doAnswer; +import static org.mockito.BDDMockito.willAnswer; import static org.mockito.Mockito.mock; import java.io.IOException; @@ -1666,7 +1666,7 @@ public class EnableRabbitIntegrationTests { @Bean public ErrorHandler errorHandler() { ErrorHandler handler = Mockito.spy(new ConditionalRejectingErrorHandler()); - doAnswer(invocation -> { + willAnswer(invocation -> { try { return invocation.callRealMethod(); } @@ -1675,7 +1675,7 @@ public class EnableRabbitIntegrationTests { errorHandlerLatch().countDown(); throw e; } - }).when(handler).handleError(Mockito.any(Throwable.class)); + }).given(handler).handleError(Mockito.any(Throwable.class)); return handler; } diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactoryTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactoryTests.java index baa35911..3fc935d5 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactoryTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 the original author or authors. + * Copyright 2010-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,15 +20,15 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willCallRealMethod; +import static org.mockito.BDDMockito.willReturn; import static org.mockito.Mockito.atLeastOnce; -import static org.mockito.Mockito.doCallRealMethod; -import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.util.Collections; import java.util.concurrent.ExecutorService; @@ -62,7 +62,7 @@ public abstract class AbstractConnectionFactoryTests { com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(com.rabbitmq.client.ConnectionFactory.class); com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); final AtomicInteger called = new AtomicInteger(0); AbstractConnectionFactory connectionFactory = createConnectionFactory(mockConnectionFactory); @@ -81,7 +81,7 @@ public abstract class AbstractConnectionFactoryTests { })); Log logger = spy(TestUtils.getPropertyValue(connectionFactory, "logger", Log.class)); - doReturn(true).when(logger).isInfoEnabled(); + willReturn(true).given(logger).isInfoEnabled(); new DirectFieldAccessor(connectionFactory).setPropertyValue("logger", logger); Connection con = connectionFactory.createConnection(); assertThat(called.get()).isEqualTo(1); @@ -122,7 +122,7 @@ public abstract class AbstractConnectionFactoryTests { com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(com.rabbitmq.client.ConnectionFactory.class); com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); final AtomicInteger called = new AtomicInteger(0); AbstractConnectionFactory connectionFactory = createConnectionFactory(mockConnectionFactory); @@ -166,11 +166,11 @@ public abstract class AbstractConnectionFactoryTests { com.rabbitmq.client.Connection mockConnection1 = mock(com.rabbitmq.client.Connection.class); com.rabbitmq.client.Connection mockConnection2 = mock(com.rabbitmq.client.Connection.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())) - .thenReturn(mockConnection1, mockConnection2); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())) + .willReturn(mockConnection1, mockConnection2); // simulate a dead connection - when(mockConnection1.isOpen()).thenReturn(false); - when(mockConnection2.createChannel()).thenReturn(mock(Channel.class)); + given(mockConnection1.isOpen()).willReturn(false); + given(mockConnection2.createChannel()).willReturn(mock(Channel.class)); AbstractConnectionFactory connectionFactory = createConnectionFactory(mockConnectionFactory); @@ -199,9 +199,9 @@ public abstract class AbstractConnectionFactoryTests { @Test public void testCreatesConnectionWithGivenFactory() { com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(com.rabbitmq.client.ConnectionFactory.class); - doCallRealMethod().when(mockConnectionFactory).params(any(ExecutorService.class)); - doCallRealMethod().when(mockConnectionFactory).setThreadFactory(any(ThreadFactory.class)); - doCallRealMethod().when(mockConnectionFactory).getThreadFactory(); + willCallRealMethod().given(mockConnectionFactory).params(any(ExecutorService.class)); + willCallRealMethod().given(mockConnectionFactory).setThreadFactory(any(ThreadFactory.class)); + willCallRealMethod().given(mockConnectionFactory).getThreadFactory(); AbstractConnectionFactory connectionFactory = createConnectionFactory(mockConnectionFactory); ThreadFactory connectionThreadFactory = new CustomizableThreadFactory("connection-thread-"); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java index 0d573b92..38b4cdfe 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java @@ -28,10 +28,9 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.willAnswer; +import static org.mockito.BDDMockito.willReturn; +import static org.mockito.BDDMockito.willThrow; import static org.mockito.Mockito.atLeastOnce; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -39,7 +38,6 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; import java.io.IOException; import java.net.URI; @@ -110,10 +108,10 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); Channel mockChannel = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.createChannel()).thenReturn(mockChannel); - when(mockChannel.isOpen()).thenReturn(true); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.createChannel()).willReturn(mockChannel); + given(mockChannel.isOpen()).willReturn(true); + given(mockConnection.isOpen()).willReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -143,10 +141,10 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); Channel mockChannel = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.createChannel()).thenReturn(mockChannel); - when(mockChannel.isOpen()).thenReturn(true); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.createChannel()).willReturn(mockChannel); + given(mockChannel.isOpen()).willReturn(true); + given(mockConnection.isOpen()).willReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -182,14 +180,14 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest Channel mockChannel2 = mock(Channel.class); Channel mockTxChannel = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); - when(mockConnection.createChannel()).thenReturn(mockChannel1, mockChannel2, mockTxChannel); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); + given(mockConnection.createChannel()).willReturn(mockChannel1, mockChannel2, mockTxChannel); - when(mockChannel1.basicGet("foo", false)).thenReturn(new GetResponse(null, null, null, 1)); - when(mockChannel2.basicGet("bar", false)).thenReturn(new GetResponse(null, null, null, 1)); - when(mockChannel1.isOpen()).thenReturn(true); - when(mockChannel2.isOpen()).thenReturn(true); + given(mockChannel1.basicGet("foo", false)).willReturn(new GetResponse(null, null, null, 1)); + given(mockChannel2.basicGet("bar", false)).willReturn(new GetResponse(null, null, null, 1)); + given(mockChannel1.isOpen()).willReturn(true); + given(mockChannel2.isOpen()).willReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -241,14 +239,14 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest Channel mockChannel2 = mock(Channel.class); Channel mockChannel3 = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.createChannel()).thenReturn(mockChannel1).thenReturn(mockChannel2).thenReturn(mockChannel3); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.createChannel()).willReturn(mockChannel1).willReturn(mockChannel2).willReturn(mockChannel3); + given(mockConnection.isOpen()).willReturn(true); // Called during physical close - when(mockChannel1.isOpen()).thenReturn(true); - when(mockChannel2.isOpen()).thenReturn(true); - when(mockChannel3.isOpen()).thenReturn(true); + given(mockChannel1.isOpen()).willReturn(true); + given(mockChannel2.isOpen()).willReturn(true); + given(mockChannel3.isOpen()).willReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -296,12 +294,12 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); Channel mockChannel1 = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.createChannel()).thenReturn(mockChannel1); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.createChannel()).willReturn(mockChannel1); + given(mockConnection.isOpen()).willReturn(true); // Called during physical close - when(mockChannel1.isOpen()).thenReturn(true); + given(mockChannel1.isOpen()).willReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -353,12 +351,12 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest return mockConnection; }).given(mockConnectionFactory).newConnection((ExecutorService) isNull(), anyString()); - when(mockConnection.createChannel()).thenReturn(mockChannel1); + given(mockConnection.createChannel()).willReturn(mockChannel1); - doAnswer(i -> !brokerDown.get()).when(mockConnection).isOpen(); + willAnswer(i -> !brokerDown.get()).given(mockConnection).isOpen(); // Called during physical close - doAnswer(i -> !brokerDown.get()).when(mockChannel1).isOpen(); + willAnswer(i -> !brokerDown.get()).given(mockChannel1).isOpen(); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setChannelCacheSize(1); @@ -404,8 +402,8 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(com.rabbitmq.client.ConnectionFactory.class); com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); final CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -467,17 +465,17 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest Channel mockChannel3 = mock(Channel.class); Channel mockChannel4 = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())) - .thenReturn(mockConnection1, mockConnection2); - when(mockConnection1.createChannel()).thenReturn(mockChannel1, mockChannel2); - when(mockConnection1.isOpen()).thenReturn(true); - when(mockConnection2.createChannel()).thenReturn(mockChannel3, mockChannel4); - when(mockConnection2.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())) + .willReturn(mockConnection1, mockConnection2); + given(mockConnection1.createChannel()).willReturn(mockChannel1, mockChannel2); + given(mockConnection1.isOpen()).willReturn(true); + given(mockConnection2.createChannel()).willReturn(mockChannel3, mockChannel4); + given(mockConnection2.isOpen()).willReturn(true); - when(mockChannel1.isOpen()).thenReturn(true); - when(mockChannel2.isOpen()).thenReturn(true); - when(mockChannel3.isOpen()).thenReturn(true); - when(mockChannel4.isOpen()).thenReturn(true); + given(mockChannel1.isOpen()).willReturn(true); + given(mockChannel2.isOpen()).willReturn(true); + given(mockChannel3.isOpen()).willReturn(true); + given(mockChannel4.isOpen()).willReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -506,9 +504,9 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest assertThat(((Semaphore) TestUtils.getPropertyValue(ccf, "checkoutPermits", Map.class).values().iterator().next()) .availablePermits()).isEqualTo(2); - when(mockConnection1.isOpen()).thenReturn(false); - when(mockChannel1.isOpen()).thenReturn(false); - when(mockChannel2.isOpen()).thenReturn(false); + given(mockConnection1.isOpen()).willReturn(false); + given(mockChannel1.isOpen()).willReturn(false); + given(mockChannel2.isOpen()).willReturn(false); con.createChannel(false).close(); con = ccf.createConnection(); @@ -541,12 +539,12 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); Channel mockChannel1 = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.createChannel()).thenReturn(mockChannel1); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.createChannel()).willReturn(mockChannel1); + given(mockConnection.isOpen()).willReturn(true); // Called during physical close - when(mockChannel1.isOpen()).thenReturn(true); + given(mockChannel1.isOpen()).willReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -602,23 +600,23 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); Channel mockChannel = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.createChannel()).thenReturn(mockChannel); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.createChannel()).willReturn(mockChannel); + given(mockConnection.isOpen()).willReturn(true); // Called during physical close - when(mockChannel.isOpen()).thenReturn(true); + given(mockChannel.isOpen()).willReturn(true); CountDownLatch confirmsLatch = new CountDownLatch(1); - doAnswer(invoc -> { + willAnswer(invoc -> { confirmsLatch.await(10, TimeUnit.SECONDS); return null; - }).when(mockChannel).waitForConfirmsOrDie(anyLong()); + }).given(mockChannel).waitForConfirmsOrDie(anyLong()); AtomicReference confirmListener = new AtomicReference<>(); - doAnswer(invoc -> { + willAnswer(invoc -> { confirmListener.set(invoc.getArgument(0)); return null; - }).when(mockChannel).addConfirmListener(any()); - when(mockChannel.getNextPublishSeqNo()).thenReturn(1L); + }).given(mockChannel).addConfirmListener(any()); + given(mockChannel.getNextPublishSeqNo()).willReturn(1L); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ExecutorService exec = Executors.newCachedThreadPool(); @@ -676,19 +674,19 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); Channel mockChannel = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.createChannel()).thenReturn(mockChannel); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.createChannel()).willReturn(mockChannel); + given(mockConnection.isOpen()).willReturn(true); AtomicBoolean open = new AtomicBoolean(true); - doAnswer(invoc -> { + willAnswer(invoc -> { return open.get(); - }).when(mockChannel).isOpen(); - when(mockChannel.getNextPublishSeqNo()).thenReturn(1L); - doAnswer(invoc -> { + }).given(mockChannel).isOpen(); + given(mockChannel.getNextPublishSeqNo()).willReturn(1L); + willAnswer(invoc -> { open.set(false); // so the logical close detects a closed delegate return null; - }).when(mockChannel).basicPublish(any(), any(), anyBoolean(), any(), any()); + }).given(mockChannel).basicPublish(any(), any(), anyBoolean(), any(), any()); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -709,11 +707,11 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest com.rabbitmq.client.Connection mockConnection1 = mock(com.rabbitmq.client.Connection.class); Channel mockChannel1 = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection1); - when(mockConnection1.createChannel()).thenReturn(mockChannel1); - when(mockConnection1.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection1); + given(mockConnection1.createChannel()).willReturn(mockChannel1); + given(mockConnection1.isOpen()).willReturn(true); - when(mockChannel1.isOpen()).thenReturn(true); + given(mockChannel1.isOpen()).willReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -758,11 +756,11 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest com.rabbitmq.client.Connection mockConnection1 = mock(com.rabbitmq.client.Connection.class); Channel mockChannel1 = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection1); - when(mockConnection1.createChannel()).thenReturn(mockChannel1); - when(mockConnection1.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection1); + given(mockConnection1.createChannel()).willReturn(mockChannel1); + given(mockConnection1.isOpen()).willReturn(true); - when(mockChannel1.isOpen()).thenReturn(true); + given(mockChannel1.isOpen()).willReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -802,13 +800,13 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest Channel mockChannel1 = mock(Channel.class); Channel mockChannel2 = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.createChannel()).thenReturn(mockChannel1).thenReturn(mockChannel2); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.createChannel()).willReturn(mockChannel1).willReturn(mockChannel2); + given(mockConnection.isOpen()).willReturn(true); // Called during physical close - when(mockChannel1.isOpen()).thenReturn(true); - when(mockChannel2.isOpen()).thenReturn(true); + given(mockChannel1.isOpen()).willReturn(true); + given(mockChannel2.isOpen()).willReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -850,13 +848,13 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest Channel mockChannel1 = mock(Channel.class); Channel mockChannel2 = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.createChannel()).thenReturn(mockChannel1).thenReturn(mockChannel2); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.createChannel()).willReturn(mockChannel1).willReturn(mockChannel2); + given(mockConnection.isOpen()).willReturn(true); // Called during physical close - when(mockChannel1.isOpen()).thenReturn(true); - when(mockChannel2.isOpen()).thenReturn(true); + given(mockChannel1.isOpen()).willReturn(true); + given(mockChannel2.isOpen()).willReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -913,15 +911,15 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest assertThat(mockChannel2).isNotSameAs(mockChannel1); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection1, mockConnection2); - when(mockConnection1.createChannel()).thenReturn(mockChannel1, mockChannel2); - when(mockConnection1.isOpen()).thenReturn(true); - when(mockConnection2.createChannel()).thenReturn(mockChannel3); - when(mockConnection2.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection1, mockConnection2); + given(mockConnection1.createChannel()).willReturn(mockChannel1, mockChannel2); + given(mockConnection1.isOpen()).willReturn(true); + given(mockConnection2.createChannel()).willReturn(mockChannel3); + given(mockConnection2.isOpen()).willReturn(true); // Called during physical close - when(mockChannel1.isOpen()).thenReturn(true); - when(mockChannel2.isOpen()).thenReturn(true); - when(mockChannel3.isOpen()).thenReturn(true); + given(mockChannel1.isOpen()).willReturn(true); + given(mockChannel2.isOpen()).willReturn(true); + given(mockChannel3.isOpen()).willReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -982,10 +980,10 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); Channel mockChannel = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); - when(mockChannel.isOpen()).thenReturn(true); - when(mockConnection.createChannel()).thenReturn(mockChannel); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); + given(mockChannel.isOpen()).willReturn(true); + given(mockConnection.createChannel()).willReturn(mockChannel); final AtomicInteger called = new AtomicInteger(0); AbstractConnectionFactory connectionFactory = createConnectionFactory(mockConnectionFactory); @@ -1022,16 +1020,16 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(com.rabbitmq.client.ConnectionFactory.class); com.rabbitmq.client.Connection mockConnection1 = mock(com.rabbitmq.client.Connection.class); - when(mockConnection1.toString()).thenReturn("conn1"); + given(mockConnection1.toString()).willReturn("conn1"); com.rabbitmq.client.Connection mockConnection2 = mock(com.rabbitmq.client.Connection.class); - when(mockConnection2.toString()).thenReturn("conn2"); + given(mockConnection2.toString()).willReturn("conn2"); Channel mockChannel = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection1, mockConnection2); - when(mockConnection1.isOpen()).thenReturn(true); - when(mockChannel.isOpen()).thenReturn(true); - when(mockConnection1.createChannel()).thenReturn(mockChannel); - when(mockConnection2.createChannel()).thenReturn(mockChannel); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection1, mockConnection2); + given(mockConnection1.isOpen()).willReturn(true); + given(mockChannel.isOpen()).willReturn(true); + given(mockConnection1.createChannel()).willReturn(mockChannel); + given(mockConnection2.createChannel()).willReturn(mockChannel); final AtomicReference created = new AtomicReference(); final AtomicReference closed = new AtomicReference(); @@ -1066,8 +1064,8 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest channel.close(); com.rabbitmq.client.Connection conDelegate = targetDelegate(con); - when(mockConnection1.isOpen()).thenReturn(false); - when(mockChannel.isOpen()).thenReturn(false); // force a connection refresh + given(mockConnection1.isOpen()).willReturn(false); + given(mockChannel.isOpen()).willReturn(false); // force a connection refresh channel.basicCancel("foo"); channel.close(); assertThat(timesClosed.get()).isEqualTo(1); @@ -1093,23 +1091,23 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest final List mockChannels = new ArrayList(); AtomicInteger connectionNumber = new AtomicInteger(); - doAnswer(invocation -> { + willAnswer(invocation -> { com.rabbitmq.client.Connection connection = mock(com.rabbitmq.client.Connection.class); AtomicInteger channelNumber = new AtomicInteger(); - doAnswer(invocation1 -> { + willAnswer(invocation1 -> { Channel channel = mock(Channel.class); - when(channel.isOpen()).thenReturn(true); + given(channel.isOpen()).willReturn(true); int channelNum = channelNumber.incrementAndGet(); - when(channel.toString()).thenReturn("mockChannel" + connectionNumber + ":" + channelNum); + given(channel.toString()).willReturn("mockChannel" + connectionNumber + ":" + channelNum); mockChannels.add(channel); return channel; - }).when(connection).createChannel(); + }).given(connection).createChannel(); int connectionNum = connectionNumber.incrementAndGet(); - when(connection.toString()).thenReturn("mockConnection" + connectionNum); - when(connection.isOpen()).thenReturn(true); + given(connection.toString()).willReturn("mockConnection" + connectionNum); + given(connection.isOpen()).willReturn(true); mockConnections.add(connection); return connection; - }).when(mockConnectionFactory).newConnection(any(ExecutorService.class), anyString()); + }).given(mockConnectionFactory).newConnection(any(ExecutorService.class), anyString()); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -1231,8 +1229,8 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest /* * Now a closed cached connection */ - when(mockConnections.get(1).isOpen()).thenReturn(false); - when(mockChannels.get(1).isOpen()).thenReturn(false); + given(mockConnections.get(1).isOpen()).willReturn(false); + given(mockChannels.get(1).isOpen()).willReturn(false); con3 = ccf.createConnection(); assertThat(closedNotification.get()).isNotNull(); assertThat(closedNotification.getAndSet(null)).isSameAs(mockConnections.get(1)); @@ -1259,7 +1257,7 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest assertThat(createNotification.get()).isNull(); assertThat(allocatedConnections).hasSize(2); assertThat(idleConnections).hasSize(1); - when(mockConnections.get(2).isOpen()).thenReturn(false); + given(mockConnections.get(2).isOpen()).willReturn(false); channel3 = con3.createChannel(false); assertThat(closedNotification.getAndSet(null)).isNotNull(); assertThat(createNotification.getAndSet(null)).isNotNull(); @@ -1287,23 +1285,23 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest final List mockChannels = new ArrayList(); AtomicInteger connectionNumber = new AtomicInteger(); - doAnswer(invocation -> { + willAnswer(invocation -> { com.rabbitmq.client.Connection connection = mock(com.rabbitmq.client.Connection.class); AtomicInteger channelNumber = new AtomicInteger(); - doAnswer(invocation1 -> { + willAnswer(invocation1 -> { Channel channel = mock(Channel.class); - when(channel.isOpen()).thenReturn(true); + given(channel.isOpen()).willReturn(true); int channelNum = channelNumber.incrementAndGet(); - when(channel.toString()).thenReturn("mockChannel" + connectionNumber + ":" + channelNum); + given(channel.toString()).willReturn("mockChannel" + connectionNumber + ":" + channelNum); mockChannels.add(channel); return channel; - }).when(connection).createChannel(); + }).given(connection).createChannel(); int connectionNum = connectionNumber.incrementAndGet(); - when(connection.toString()).thenReturn("mockConnection" + connectionNum); - when(connection.isOpen()).thenReturn(true); + given(connection.toString()).willReturn("mockConnection" + connectionNum); + given(connection.isOpen()).willReturn(true); mockConnections.add(connection); return connection; - }).when(mockConnectionFactory).newConnection(any(ExecutorService.class), anyString()); + }).given(mockConnectionFactory).newConnection(any(ExecutorService.class), anyString()); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -1434,7 +1432,7 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest /* * Now a closed cached connection */ - when(mockConnections.get(1).isOpen()).thenReturn(false); + given(mockConnections.get(1).isOpen()).willReturn(false); con3 = ccf.createConnection(); assertThat(closedNotification.get()).isNotNull(); assertThat(closedNotification.getAndSet(null)).isSameAs(mockConnections.get(1)); @@ -1458,7 +1456,7 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest assertThat(createNotification.get()).isNull(); assertThat(allocatedConnections).hasSize(2); assertThat(idleConnections).hasSize(1); - when(mockConnections.get(0).isOpen()).thenReturn(false); + given(mockConnections.get(0).isOpen()).willReturn(false); channel3 = con3.createChannel(false); assertThat(closedNotification.getAndSet(null)).isNotNull(); assertThat(createNotification.getAndSet(null)).isNotNull(); @@ -1500,23 +1498,23 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest final List mockChannels = new ArrayList(); AtomicInteger connectionNumber = new AtomicInteger(); - doAnswer(invocation -> { + willAnswer(invocation -> { com.rabbitmq.client.Connection connection = mock(com.rabbitmq.client.Connection.class); AtomicInteger channelNumber = new AtomicInteger(); - doAnswer(invocation1 -> { + willAnswer(invocation1 -> { Channel channel = mock(Channel.class); - when(channel.isOpen()).thenReturn(true); + given(channel.isOpen()).willReturn(true); int channelNum = channelNumber.incrementAndGet(); - when(channel.toString()).thenReturn("mockChannel" + channelNum); + given(channel.toString()).willReturn("mockChannel" + channelNum); mockChannels.add(channel); return channel; - }).when(connection).createChannel(); + }).given(connection).createChannel(); int connectionNum = connectionNumber.incrementAndGet(); - when(connection.toString()).thenReturn("mockConnection" + connectionNum); - when(connection.isOpen()).thenReturn(true); + given(connection.toString()).willReturn("mockConnection" + connectionNum); + given(connection.isOpen()).willReturn(true); mockConnections.add(connection); return connection; - }).when(mockConnectionFactory).newConnection(any(ExecutorService.class), anyString()); + }).given(mockConnectionFactory).newConnection(any(ExecutorService.class), anyString()); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -1540,8 +1538,8 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest assertThat(allocatedConnections).hasSize(3); assertThat(idleConnections).hasSize(3); - when(mockConnections.get(0).isOpen()).thenReturn(false); - when(mockConnections.get(1).isOpen()).thenReturn(false); + given(mockConnections.get(0).isOpen()).willReturn(false); + given(mockConnections.get(1).isOpen()).willReturn(false); Connection conn4 = ccf.createConnection(); assertThat(allocatedConnections).hasSize(3); assertThat(idleConnections).hasSize(2); @@ -1574,10 +1572,10 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); Channel mockChannel = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.createChannel()).thenReturn(mockChannel); - when(mockChannel.isOpen()).thenReturn(true); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.createChannel()).willReturn(mockChannel); + given(mockChannel.isOpen()).willReturn(true); + given(mockConnection.isOpen()).willReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(executor); @@ -1588,12 +1586,12 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest Channel channel = con.createChannel(false); RabbitUtils.setPhysicalCloseRequired(channel, true); - when(mockChannel.isOpen()).thenReturn(false); + given(mockChannel.isOpen()).willReturn(false); final CountDownLatch physicalCloseLatch = new CountDownLatch(1); - doAnswer(i -> { + willAnswer(i -> { physicalCloseLatch.countDown(); return null; - }).when(mockChannel).close(); + }).given(mockChannel).close(); channel.close(); RabbitUtils.setPhysicalCloseRequired(channel, false); con.close(); // should be ignored @@ -1653,7 +1651,7 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest @Test public void setAddressesTwoHosts() throws Exception { ConnectionFactory mock = mock(com.rabbitmq.client.ConnectionFactory.class); - doReturn(true).when(mock).isAutomaticRecoveryEnabled(); + willReturn(true).given(mock).isAutomaticRecoveryEnabled(); CachingConnectionFactory ccf = new CachingConnectionFactory(mock); ccf.setAddresses("mq1,mq2"); ccf.createConnection(); @@ -1693,10 +1691,10 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); Channel mockChannel = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); - when(mockConnection.createChannel()).thenReturn(mockChannel); - when(mockChannel.isOpen()).thenReturn(true).thenReturn(false); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); + given(mockConnection.createChannel()).willReturn(mockChannel); + given(mockChannel.isOpen()).willReturn(true).willReturn(false); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -1718,14 +1716,14 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); Channel mockChannel = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); - when(mockConnection.createChannel()).thenReturn(mockChannel); - when(mockChannel.isOpen()).thenReturn(true); - doThrow(new ShutdownSignalException(true, false, new com.rabbitmq.client.AMQP.Connection.Close.Builder() + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); + given(mockConnection.createChannel()).willReturn(mockChannel); + given(mockChannel.isOpen()).willReturn(true); + willThrow(new ShutdownSignalException(true, false, new com.rabbitmq.client.AMQP.Connection.Close.Builder() .replyCode(200) .replyText("OK") - .build(), null)).when(mockChannel).close(); + .build(), null)).given(mockChannel).close(); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setPublisherReturns(true); @@ -1854,11 +1852,11 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest Channel mockChannel = mock(Channel.class); AddressResolver resolver = () -> Collections.singletonList(Address.parseAddress("foo:5672")); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), eq(resolver), anyString())) - .thenReturn(mockConnection); - when(mockConnection.createChannel()).thenReturn(mockChannel); - when(mockChannel.isOpen()).thenReturn(true); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), eq(resolver), anyString())) + .willReturn(mockConnection); + given(mockConnection.createChannel()).willReturn(mockChannel); + given(mockChannel.isOpen()).willReturn(true); + given(mockConnection.isOpen()).willReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/ClientRecoveryCompatibilityTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/ClientRecoveryCompatibilityTests.java index 912f9df9..fafe709d 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/ClientRecoveryCompatibilityTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/ClientRecoveryCompatibilityTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,11 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.doAnswer; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.util.concurrent.ExecutorService; @@ -44,14 +44,14 @@ public class ClientRecoveryCompatibilityTests { @Test public void testDefeatRecovery() throws Exception { final Channel channel1 = mock(Channel.class); - when(channel1.isOpen()).thenReturn(true); + given(channel1.isOpen()).willReturn(true); final Channel channel2 = mock(Channel.class); - when(channel2.isOpen()).thenReturn(true); + given(channel2.isOpen()).willReturn(true); final com.rabbitmq.client.Connection rabbitConn = mock(AutorecoveringConnection.class); - when(rabbitConn.isOpen()).thenReturn(true); + given(rabbitConn.isOpen()).willReturn(true); com.rabbitmq.client.ConnectionFactory cf = mock(com.rabbitmq.client.ConnectionFactory.class); - doAnswer(invocation -> rabbitConn).when(cf).newConnection(any(ExecutorService.class), anyString()); - when(rabbitConn.createChannel()).thenReturn(channel1).thenReturn(channel2); + willAnswer(invocation -> rabbitConn).given(cf).newConnection(any(ExecutorService.class), anyString()); + given(rabbitConn.createChannel()).willReturn(channel1).willReturn(channel2); CachingConnectionFactory ccf = new CachingConnectionFactory(cf); ccf.setExecutor(mock(ExecutorService.class)); @@ -67,8 +67,8 @@ public class ClientRecoveryCompatibilityTests { channel.close(); conn2.close(); - when(rabbitConn.isOpen()).thenReturn(false).thenReturn(true); - when(channel1.isOpen()).thenReturn(false); + given(rabbitConn.isOpen()).willReturn(false).willReturn(true); + given(channel1.isOpen()).willReturn(false); conn2 = ccf.createConnection(); try { conn2.createChannel(false); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/LocalizedQueueConnectionFactoryTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/LocalizedQueueConnectionFactoryTests.java index 7ecb7ae6..e7b55e55 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/LocalizedQueueConnectionFactoryTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/LocalizedQueueConnectionFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2019 the original author or authors. + * Copyright 2015-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,13 +21,13 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyMap; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willAnswer; +import static org.mockito.BDDMockito.willReturn; import static org.mockito.Mockito.atLeast; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.util.HashMap; import java.util.List; @@ -103,9 +103,9 @@ public class LocalizedQueueConnectionFactoryTests { String[] admins = TestUtils.getPropertyValue(lqcf, "adminUris", String[].class); assertThat(admins).containsExactly(adminUris); Log logger = spy(TestUtils.getPropertyValue(lqcf, "logger", Log.class)); - doReturn(true).when(logger).isInfoEnabled(); + willReturn(true).given(logger).isInfoEnabled(); new DirectFieldAccessor(lqcf).setPropertyValue("logger", logger); - doAnswer(new CallsRealMethods()).when(logger).debug(anyString()); + willAnswer(new CallsRealMethods()).given(logger).debug(anyString()); ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(lqcf); container.setQueueNames("q"); @@ -148,7 +148,7 @@ public class LocalizedQueueConnectionFactoryTests { Client client = mock(Client.class); QueueInfo queueInfo = new QueueInfo(); queueInfo.setNode(node); - when(client.getQueue("/", "q")).thenReturn(queueInfo); + given(client.getQueue("/", "q")).willReturn(queueInfo); return client; } @@ -176,11 +176,11 @@ public class LocalizedQueueConnectionFactoryTests { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); - when(connectionFactory.createConnection()).thenReturn(connection); - when(connection.createChannel(false)).thenReturn(channel); - when(connection.isOpen()).thenReturn(true, false); - when(channel.isOpen()).thenReturn(true, false); - doAnswer(invocation -> { + given(connectionFactory.createConnection()).willReturn(connection); + given(connection.createChannel(false)).willReturn(channel); + given(connection.isOpen()).willReturn(true, false); + given(channel.isOpen()).willReturn(true, false); + willAnswer(invocation -> { String tag = UUID.randomUUID().toString(); consumers.put(address, invocation.getArgument(6)); consumerTags.put(address, tag); @@ -188,9 +188,9 @@ public class LocalizedQueueConnectionFactoryTests { latch.countDown(); } return tag; - }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), + }).given(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); - when(connectionFactory.getHost()).thenReturn(address); + given(connectionFactory.getHost()).willReturn(address); this.channels.put(address, channel); return connectionFactory; } diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/SingleConnectionFactoryTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/SingleConnectionFactoryTests.java index 8f38e5f3..b1fe3753 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/SingleConnectionFactoryTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/SingleConnectionFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,11 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.util.Collections; import java.util.concurrent.ExecutorService; @@ -55,9 +55,9 @@ public class SingleConnectionFactoryTests extends AbstractConnectionFactoryTests com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); Channel mockChannel = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); - when(mockConnection.createChannel()).thenReturn(mockChannel); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); + given(mockConnection.createChannel()).willReturn(mockChannel); final AtomicInteger called = new AtomicInteger(0); AbstractConnectionFactory connectionFactory = createConnectionFactory(mockConnectionFactory); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/BatchingRabbitTemplateTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/BatchingRabbitTemplateTests.java index 5c16815d..c55c6216 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/BatchingRabbitTemplateTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/BatchingRabbitTemplateTests.java @@ -20,8 +20,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.doReturn; +import static org.mockito.BDDMockito.willDoNothing; +import static org.mockito.BDDMockito.willReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; @@ -351,8 +351,8 @@ public class BatchingRabbitTemplateTests { container.afterPropertiesSet(); container.start(); Log logger = spy(TestUtils.getPropertyValue(errorHandler, "logger", Log.class)); - doReturn(true).when(logger).isWarnEnabled(); - doNothing().when(logger).warn(anyString(), any(Throwable.class)); + willReturn(true).given(logger).isWarnEnabled(); + willDoNothing().given(logger).warn(anyString(), any(Throwable.class)); new DirectFieldAccessor(errorHandler).setPropertyValue("logger", logger); try { RabbitTemplate template = new RabbitTemplate(); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitAdminDeclarationTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitAdminDeclarationTests.java index 8b2d09f4..3d82f4c1 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitAdminDeclarationTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitAdminDeclarationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,11 +27,9 @@ import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.willAnswer; import static org.mockito.BDDMockito.willReturn; -import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.io.IOException; import java.util.ArrayList; @@ -78,15 +76,15 @@ public class RabbitAdminDeclarationTests { ConnectionFactory cf = mock(ConnectionFactory.class); Connection conn = mock(Connection.class); Channel channel = mock(Channel.class); - when(cf.createConnection()).thenReturn(conn); - when(conn.createChannel(false)).thenReturn(channel); - when(channel.queueDeclare("foo", true, false, false, new HashMap<>())) - .thenReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); + given(cf.createConnection()).willReturn(conn); + given(conn.createChannel(false)).willReturn(channel); + given(channel.queueDeclare("foo", true, false, false, new HashMap<>())) + .willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); final AtomicReference listener = new AtomicReference(); - doAnswer(invocation -> { + willAnswer(invocation -> { listener.set((ConnectionListener) invocation.getArguments()[0]); return null; - }).when(cf).addConnectionListener(any(ConnectionListener.class)); + }).given(cf).addConnectionListener(any(ConnectionListener.class)); RabbitAdmin admin = new RabbitAdmin(cf); GenericApplicationContext context = new GenericApplicationContext(); Queue queue = new Queue("foo"); @@ -113,22 +111,22 @@ public class RabbitAdminDeclarationTests { final List mockChannels = new ArrayList(); AtomicInteger connectionNumber = new AtomicInteger(); - doAnswer(invocation -> { + willAnswer(invocation -> { com.rabbitmq.client.Connection connection = mock(com.rabbitmq.client.Connection.class); AtomicInteger channelNumber = new AtomicInteger(); - doAnswer(invocation1 -> { + willAnswer(invocation1 -> { Channel channel = mock(Channel.class); - when(channel.isOpen()).thenReturn(true); + given(channel.isOpen()).willReturn(true); int channelNum = channelNumber.incrementAndGet(); - when(channel.toString()).thenReturn("mockChannel" + channelNum); + given(channel.toString()).willReturn("mockChannel" + channelNum); mockChannels.add(channel); return channel; - }).when(connection).createChannel(); + }).given(connection).createChannel(); int connectionNum = connectionNumber.incrementAndGet(); - when(connection.toString()).thenReturn("mockConnection" + connectionNum); - when(connection.isOpen()).thenReturn(true); + given(connection.toString()).willReturn("mockConnection" + connectionNum); + given(connection.isOpen()).willReturn(true); return connection; - }).when(mockConnectionFactory).newConnection((ExecutorService) null); + }).given(mockConnectionFactory).newConnection((ExecutorService) null); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setCacheMode(CacheMode.CONNECTION); @@ -152,15 +150,15 @@ public class RabbitAdminDeclarationTests { ConnectionFactory cf = mock(ConnectionFactory.class); Connection conn = mock(Connection.class); Channel channel = mock(Channel.class); - when(cf.createConnection()).thenReturn(conn); - when(conn.createChannel(false)).thenReturn(channel); - when(channel.queueDeclare("foo", true, false, false, new HashMap<>())) - .thenReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); + given(cf.createConnection()).willReturn(conn); + given(conn.createChannel(false)).willReturn(channel); + given(channel.queueDeclare("foo", true, false, false, new HashMap<>())) + .willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); final AtomicReference listener = new AtomicReference(); - doAnswer(invocation -> { + willAnswer(invocation -> { listener.set(invocation.getArgument(0)); return null; - }).when(cf).addConnectionListener(any(ConnectionListener.class)); + }).given(cf).addConnectionListener(any(ConnectionListener.class)); RabbitAdmin admin = new RabbitAdmin(cf); GenericApplicationContext context = new GenericApplicationContext(); Queue queue = new Queue("foo"); @@ -189,14 +187,14 @@ public class RabbitAdminDeclarationTests { ConnectionFactory cf = mock(ConnectionFactory.class); Connection conn = mock(Connection.class); Channel channel = mock(Channel.class); - when(cf.createConnection()).thenReturn(conn); - when(conn.createChannel(false)).thenReturn(channel); - when(channel.queueDeclare("foo", true, false, false, null)).thenReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); + given(cf.createConnection()).willReturn(conn); + given(conn.createChannel(false)).willReturn(channel); + given(channel.queueDeclare("foo", true, false, false, null)).willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); final AtomicReference listener = new AtomicReference(); - doAnswer(invocation -> { + willAnswer(invocation -> { listener.set(invocation.getArgument(0)); return null; - }).when(cf).addConnectionListener(any(ConnectionListener.class)); + }).given(cf).addConnectionListener(any(ConnectionListener.class)); RabbitAdmin admin = new RabbitAdmin(cf); RabbitAdmin other = new RabbitAdmin(cf); GenericApplicationContext context = new GenericApplicationContext(); @@ -227,14 +225,14 @@ public class RabbitAdminDeclarationTests { ConnectionFactory cf = mock(ConnectionFactory.class); Connection conn = mock(Connection.class); Channel channel = mock(Channel.class); - when(cf.createConnection()).thenReturn(conn); - when(conn.createChannel(false)).thenReturn(channel); - when(channel.queueDeclare("foo", true, false, false, null)).thenReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); + given(cf.createConnection()).willReturn(conn); + given(conn.createChannel(false)).willReturn(channel); + given(channel.queueDeclare("foo", true, false, false, null)).willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); final AtomicReference listener = new AtomicReference(); - doAnswer(invocation -> { + willAnswer(invocation -> { listener.set(invocation.getArgument(0)); return null; - }).when(cf).addConnectionListener(any(ConnectionListener.class)); + }).given(cf).addConnectionListener(any(ConnectionListener.class)); RabbitAdmin admin = new RabbitAdmin(cf); GenericApplicationContext context = new GenericApplicationContext(); Queue queue = new Queue("foo"); @@ -371,45 +369,45 @@ public class RabbitAdminDeclarationTests { @Bean public ConnectionFactory cf1() throws IOException { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); - when(connectionFactory.createConnection()).thenReturn(conn1); - when(conn1.createChannel(false)).thenReturn(channel1); + given(connectionFactory.createConnection()).willReturn(conn1); + given(conn1.createChannel(false)).willReturn(channel1); willAnswer(inv -> { return new AMQImpl.Queue.DeclareOk(inv.getArgument(0), 0, 0); }).given(channel1).queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), any()); - doAnswer(invocation -> { + willAnswer(invocation -> { listener1 = invocation.getArgument(0); return null; - }).when(connectionFactory).addConnectionListener(any(ConnectionListener.class)); + }).given(connectionFactory).addConnectionListener(any(ConnectionListener.class)); return connectionFactory; } @Bean public ConnectionFactory cf2() throws IOException { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); - when(connectionFactory.createConnection()).thenReturn(conn2); - when(conn2.createChannel(false)).thenReturn(channel2); + given(connectionFactory.createConnection()).willReturn(conn2); + given(conn2.createChannel(false)).willReturn(channel2); willAnswer(inv -> { return new AMQImpl.Queue.DeclareOk(inv.getArgument(0), 0, 0); }).given(channel2).queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), any()); - doAnswer(invocation -> { + willAnswer(invocation -> { listener2 = invocation.getArgument(0); return null; - }).when(connectionFactory).addConnectionListener(any(ConnectionListener.class)); + }).given(connectionFactory).addConnectionListener(any(ConnectionListener.class)); return connectionFactory; } @Bean public ConnectionFactory cf3() throws IOException { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); - when(connectionFactory.createConnection()).thenReturn(conn3); - when(conn3.createChannel(false)).thenReturn(channel3); + given(connectionFactory.createConnection()).willReturn(conn3); + given(conn3.createChannel(false)).willReturn(channel3); willAnswer(inv -> { return new AMQImpl.Queue.DeclareOk(inv.getArgument(0), 0, 0); }).given(channel3).queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), any()); - doAnswer(invocation -> { + willAnswer(invocation -> { listener3 = invocation.getArgument(0); return null; - }).when(connectionFactory).addConnectionListener(any(ConnectionListener.class)); + }).given(connectionFactory).addConnectionListener(any(ConnectionListener.class)); return connectionFactory; } diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitAdminTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitAdminTests.java index cd55b385..bef0e102 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitAdminTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitAdminTests.java @@ -34,7 +34,6 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; -import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.Collections; @@ -184,12 +183,12 @@ public class RabbitAdminTests { queues.put("adQ", new Queue("testq.ad", true, false, true)); queues.put("exclQ", new Queue("testq.excl", true, true, false)); queues.put("allQ", new Queue("testq.all", false, true, true)); - when(ctx.getBeansOfType(Queue.class)).thenReturn(queues); + given(ctx.getBeansOfType(Queue.class)).willReturn(queues); Map exchanges = new HashMap(); exchanges.put("nonDurEx", new DirectExchange("testex.nonDur", false, false)); exchanges.put("adEx", new DirectExchange("testex.ad", true, true)); exchanges.put("allEx", new DirectExchange("testex.all", false, true)); - when(ctx.getBeansOfType(Exchange.class)).thenReturn(exchanges); + given(ctx.getBeansOfType(Exchange.class)).willReturn(exchanges); rabbitAdmin.setApplicationContext(ctx); rabbitAdmin.afterPropertiesSet(); Log logger = spy(TestUtils.getPropertyValue(rabbitAdmin, "logger", Log.class)); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateHeaderTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateHeaderTests.java index 797a3dab..a6926c79 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateHeaderTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateHeaderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,9 +19,9 @@ package org.springframework.amqp.rabbit.core; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.doAnswer; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willAnswer; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.List; @@ -66,9 +66,9 @@ public class RabbitTemplateHeaderTests { Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); - when(mockConnection.createChannel()).thenReturn(mockChannel); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); + given(mockConnection.createChannel()).willReturn(mockChannel); SingleConnectionFactory connectionFactory = new SingleConnectionFactory(mockConnectionFactory); connectionFactory.setExecutor(mock(ExecutorService.class)); @@ -84,7 +84,7 @@ public class RabbitTemplateHeaderTests { Message message = new Message("Hello, world!".getBytes(), messageProperties); final AtomicReference replyTo = new AtomicReference(); final AtomicReference correlationId = new AtomicReference(); - doAnswer(invocation -> { + willAnswer(invocation -> { BasicProperties basicProps = invocation.getArgument(3); replyTo.set(basicProps.getReplyTo()); if (standardHeader) { @@ -98,7 +98,7 @@ public class RabbitTemplateHeaderTests { Message replyMessage = new Message("!dlrow olleH".getBytes(), springProps); template.onMessage(replyMessage); return null; - }).when(mockChannel).basicPublish(any(String.class), any(String.class), Mockito.anyBoolean(), + }).given(mockChannel).basicPublish(any(String.class), any(String.class), Mockito.anyBoolean(), any(BasicProperties.class), any(byte[].class)); Message reply = template.sendAndReceive(message); assertThat(reply).isNotNull(); @@ -121,9 +121,9 @@ public class RabbitTemplateHeaderTests { Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); - when(mockConnection.createChannel()).thenReturn(mockChannel); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); + given(mockConnection.createChannel()).willReturn(mockChannel); SingleConnectionFactory connectionFactory = new SingleConnectionFactory(mockConnectionFactory); connectionFactory.setExecutor(mock(ExecutorService.class)); @@ -139,7 +139,7 @@ public class RabbitTemplateHeaderTests { Message message = new Message("Hello, world!".getBytes(), messageProperties); final AtomicReference replyTo = new AtomicReference(); final AtomicReference correlationId = new AtomicReference(); - doAnswer(invocation -> { + willAnswer(invocation -> { BasicProperties basicProps = invocation.getArgument(3); replyTo.set(basicProps.getReplyTo()); correlationId.set(basicProps.getCorrelationId()); @@ -148,7 +148,7 @@ public class RabbitTemplateHeaderTests { Message replyMessage = new Message("!dlrow olleH".getBytes(), springProps); template.onMessage(replyMessage); return null; - }).when(mockChannel).basicPublish(any(String.class), any(String.class), Mockito.anyBoolean(), + }).given(mockChannel).basicPublish(any(String.class), any(String.class), Mockito.anyBoolean(), any(BasicProperties.class), any(byte[].class)); Message reply = template.sendAndReceive(message); assertThat(reply).isNotNull(); @@ -167,9 +167,9 @@ public class RabbitTemplateHeaderTests { Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); - when(mockConnection.createChannel()).thenReturn(mockChannel); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); + given(mockConnection.createChannel()).willReturn(mockChannel); SingleConnectionFactory scf = new SingleConnectionFactory(mockConnectionFactory); scf.setExecutor(mock(ExecutorService.class)); @@ -187,7 +187,7 @@ public class RabbitTemplateHeaderTests { final List nestedReplyTo = new ArrayList(); final List nestedCorrelation = new ArrayList(); final String replyAddress3 = "replyTo3"; - doAnswer(invocation -> { + willAnswer(invocation -> { BasicProperties basicProps = invocation.getArgument(3); nestedReplyTo.add(basicProps.getReplyTo()); nestedCorrelation.add(basicProps.getCorrelationId()); @@ -203,7 +203,7 @@ public class RabbitTemplateHeaderTests { } template.onMessage(replyMessage); return null; - }).when(mockChannel).basicPublish(any(String.class), any(String.class), Mockito.anyBoolean(), + }).given(mockChannel).basicPublish(any(String.class), any(String.class), Mockito.anyBoolean(), any(BasicProperties.class), any(byte[].class)); Message reply = template.sendAndReceive(message); assertThat(reply).isNotNull(); @@ -224,9 +224,9 @@ public class RabbitTemplateHeaderTests { Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); - when(mockConnection.createChannel()).thenReturn(mockChannel); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); + given(mockConnection.createChannel()).willReturn(mockChannel); SingleConnectionFactory connectionFactory = new SingleConnectionFactory(mockConnectionFactory); connectionFactory.setExecutor(mock(ExecutorService.class)); @@ -243,7 +243,7 @@ public class RabbitTemplateHeaderTests { Message message = new Message("Hello, world!".getBytes(), messageProperties); final AtomicReference replyTo = new AtomicReference(); final AtomicReference correlationId = new AtomicReference(); - doAnswer(invocation -> { + willAnswer(invocation -> { BasicProperties basicProps = invocation.getArgument(3); replyTo.set(basicProps.getReplyTo()); correlationId.set((String) basicProps.getHeaders().get(CORRELATION_HEADER)); @@ -253,7 +253,7 @@ public class RabbitTemplateHeaderTests { Message replyMessage = new Message("!dlrow olleH".getBytes(), springProps); template.onMessage(replyMessage); return null; - }).when(mockChannel).basicPublish(any(String.class), any(String.class), Mockito.anyBoolean(), + }).given(mockChannel).basicPublish(any(String.class), any(String.class), Mockito.anyBoolean(), any(BasicProperties.class), any(byte[].class)); Message reply = template.sendAndReceive(message); assertThat(reply).isNotNull(); @@ -273,9 +273,9 @@ public class RabbitTemplateHeaderTests { Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); - when(mockConnection.createChannel()).thenReturn(mockChannel); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); + given(mockConnection.createChannel()).willReturn(mockChannel); SingleConnectionFactory connectionFactory = new SingleConnectionFactory(mockConnectionFactory); connectionFactory.setExecutor(mock(ExecutorService.class)); @@ -294,7 +294,7 @@ public class RabbitTemplateHeaderTests { final List nestedReplyTo = new ArrayList(); final List nestedCorrelation = new ArrayList(); final String replyTo3 = "replyTo3"; - doAnswer(invocation -> { + willAnswer(invocation -> { BasicProperties basicProps = invocation.getArgument(3); nestedReplyTo.add(basicProps.getReplyTo()); nestedCorrelation.add(basicProps.getCorrelationId()); @@ -310,7 +310,7 @@ public class RabbitTemplateHeaderTests { } template.onMessage(replyMessage); return null; - }).when(mockChannel).basicPublish(any(String.class), any(String.class), Mockito.anyBoolean(), + }).given(mockChannel).basicPublish(any(String.class), any(String.class), Mockito.anyBoolean(), any(BasicProperties.class), any(byte[].class)); Message reply = template.sendAndReceive(message); assertThat(reply).isNotNull(); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateIntegrationTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateIntegrationTests.java index 50edef78..7d95d463 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateIntegrationTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateIntegrationTests.java @@ -23,14 +23,14 @@ import static org.awaitility.Awaitility.await; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willAnswer; +import static org.mockito.BDDMockito.willReturn; import static org.mockito.Mockito.atLeast; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.io.IOException; import java.lang.reflect.Field; @@ -179,8 +179,8 @@ public class RabbitTemplateIntegrationTests { this.template.setSendConnectionFactorySelectorExpression(new LiteralExpression("foo")); BeanFactory bf = mock(BeanFactory.class); ConnectionFactory cf = mock(ConnectionFactory.class); - when(cf.getUsername()).thenReturn("guest"); - when(bf.getBean("cf")).thenReturn(cf); + given(cf.getUsername()).willReturn("guest"); + given(bf.getBean("cf")).willReturn(cf); this.template.setBeanFactory(bf); this.template.setBeanName(info.getDisplayName() + ".RabbitTemplate"); this.testName = info.getDisplayName(); @@ -333,7 +333,7 @@ public class RabbitTemplateIntegrationTests { } Connection connection = spy(connectionFactory.createConnection()); - when(connection.createChannel(anyBoolean())).then( + given(connection.createChannel(anyBoolean())).willAnswer( invocation -> new MockChannel((Channel) invocation.callRealMethod())); DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory); @@ -746,17 +746,17 @@ public class RabbitTemplateIntegrationTests { fields[0] = field; }, field -> field.getName().equals("logger")); Log logger = Mockito.mock(Log.class); - when(logger.isTraceEnabled()).thenReturn(true); + given(logger.isTraceEnabled()).willReturn(true); final AtomicBoolean execConfiguredOk = new AtomicBoolean(); - doAnswer(invocation -> { + willAnswer(invocation -> { String log = invocation.getArgument(0); if (log.startsWith("Message received") && Thread.currentThread().getName().startsWith(execName)) { execConfiguredOk.set(true); } return null; - }).when(logger).trace(anyString()); + }).given(logger).trace(anyString()); final RabbitTemplate template = createSendAndReceiveRabbitTemplate(connectionFactory); ReflectionUtils.setField(fields[0], template, logger); template.setRoutingKey(ROUTE); @@ -1415,7 +1415,7 @@ public class RabbitTemplateIntegrationTests { public void testDebugLogOnPassiveDeclaration() { CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost"); Log logger = spy(TestUtils.getPropertyValue(connectionFactory, "logger", Log.class)); - doReturn(true).when(logger).isDebugEnabled(); + willReturn(true).given(logger).isDebugEnabled(); new DirectFieldAccessor(connectionFactory).setPropertyValue("logger", logger); RabbitTemplate template = new RabbitTemplate(connectionFactory); final String queueName = UUID.randomUUID().toString(); @@ -1464,9 +1464,9 @@ public class RabbitTemplateIntegrationTests { @Test public void testRouting() throws Exception { Connection connection1 = mock(Connection.class); - when(this.cf1.createConnection()).thenReturn(connection1); + given(this.cf1.createConnection()).willReturn(connection1); Channel channel1 = mock(Channel.class); - when(connection1.createChannel(false)).thenReturn(channel1); + given(connection1.createChannel(false)).willReturn(channel1); this.routingTemplate.convertAndSend("exchange", "routingKey", "xyz", message -> { message.getMessageProperties().setHeader("cfKey", "foo"); return message; @@ -1475,9 +1475,9 @@ public class RabbitTemplateIntegrationTests { any(byte[].class)); Connection connection2 = mock(Connection.class); - when(this.cf2.createConnection()).thenReturn(connection2); + given(this.cf2.createConnection()).willReturn(connection2); Channel channel2 = mock(Channel.class); - when(connection2.createChannel(false)).thenReturn(channel2); + given(connection2.createChannel(false)).willReturn(channel2); this.routingTemplate.convertAndSend("exchange", "routingKey", "xyz", message -> { message.getMessageProperties().setHeader("cfKey", "bar"); return message; diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplatePublisherCallbacksIntegrationTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplatePublisherCallbacksIntegrationTests.java index 45ae136b..be5a80c8 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplatePublisherCallbacksIntegrationTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplatePublisherCallbacksIntegrationTests.java @@ -21,13 +21,13 @@ import static org.awaitility.Awaitility.await; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.doReturn; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willAnswer; +import static org.mockito.BDDMockito.willReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.Collection; @@ -318,13 +318,13 @@ public class RabbitTemplatePublisherCallbacksIntegrationTests { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); - when(mockChannel.isOpen()).thenReturn(true); + given(mockChannel.isOpen()).willReturn(true); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); - doReturn(new PublisherCallbackChannelImpl(mockChannel, this.executorService)) - .when(mockConnection) + willReturn(new PublisherCallbackChannelImpl(mockChannel, this.executorService)) + .given(mockConnection) .createChannel(); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); @@ -350,17 +350,17 @@ public class RabbitTemplatePublisherCallbacksIntegrationTests { Connection mockConnection = mock(Connection.class); Channel mockChannel1 = mock(Channel.class); Channel mockChannel2 = mock(Channel.class); - when(mockChannel1.isOpen()).thenReturn(true); - when(mockChannel2.isOpen()).thenReturn(true); - when(mockChannel1.getNextPublishSeqNo()).thenReturn(1L, 2L, 3L, 4L); - when(mockChannel2.getNextPublishSeqNo()).thenReturn(1L, 2L, 3L, 4L); + given(mockChannel1.isOpen()).willReturn(true); + given(mockChannel2.isOpen()).willReturn(true); + given(mockChannel1.getNextPublishSeqNo()).willReturn(1L, 2L, 3L, 4L); + given(mockChannel2.getNextPublishSeqNo()).willReturn(1L, 2L, 3L, 4L); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); PublisherCallbackChannelImpl channel1 = new PublisherCallbackChannelImpl(mockChannel1, this.executorService); PublisherCallbackChannelImpl channel2 = new PublisherCallbackChannelImpl(mockChannel2, this.executorService); - when(mockConnection.createChannel()).thenReturn(channel1).thenReturn(channel2); + given(mockConnection.createChannel()).willReturn(channel1).willReturn(channel2); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -420,17 +420,17 @@ public class RabbitTemplatePublisherCallbacksIntegrationTests { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); - when(mockChannel.isOpen()).thenReturn(true); + given(mockChannel.isOpen()).willReturn(true); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); - doReturn(new PublisherCallbackChannelImpl(mockChannel, this.executorService)) - .when(mockConnection) + willReturn(new PublisherCallbackChannelImpl(mockChannel, this.executorService)) + .given(mockConnection) .createChannel(); final AtomicLong count = new AtomicLong(); - doAnswer(invocation -> count.incrementAndGet()).when(mockChannel).getNextPublishSeqNo(); + willAnswer(invocation -> count.incrementAndGet()).given(mockChannel).getNextPublishSeqNo(); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -463,16 +463,16 @@ public class RabbitTemplatePublisherCallbacksIntegrationTests { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); - when(mockChannel.isOpen()).thenReturn(true); + given(mockChannel.isOpen()).willReturn(true); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); PublisherCallbackChannelImpl callbackChannel = new PublisherCallbackChannelImpl(mockChannel, this.executorService); - when(mockConnection.createChannel()).thenReturn(callbackChannel); + given(mockConnection.createChannel()).willReturn(callbackChannel); final AtomicLong count = new AtomicLong(); - doAnswer(invocation -> count.incrementAndGet()).when(mockChannel).getNextPublishSeqNo(); + willAnswer(invocation -> count.incrementAndGet()).given(mockChannel).getNextPublishSeqNo(); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -503,16 +503,16 @@ public class RabbitTemplatePublisherCallbacksIntegrationTests { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); - when(mockChannel.isOpen()).thenReturn(true); + given(mockChannel.isOpen()).willReturn(true); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); PublisherCallbackChannelImpl callbackChannel = new PublisherCallbackChannelImpl(mockChannel, this.executorService); - when(mockConnection.createChannel()).thenReturn(callbackChannel); + given(mockConnection.createChannel()).willReturn(callbackChannel); final AtomicLong count = new AtomicLong(); - doAnswer(invocation -> count.incrementAndGet()).when(mockChannel).getNextPublishSeqNo(); + willAnswer(invocation -> count.incrementAndGet()).given(mockChannel).getNextPublishSeqNo(); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -564,14 +564,14 @@ public class RabbitTemplatePublisherCallbacksIntegrationTests { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); - when(mockChannel.isOpen()).thenReturn(true); - when(mockChannel.getNextPublishSeqNo()).thenReturn(1L, 2L, 3L, 4L); + given(mockChannel.isOpen()).willReturn(true); + given(mockChannel.getNextPublishSeqNo()).willReturn(1L, 2L, 3L, 4L); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); final PublisherCallbackChannelImpl channel = new PublisherCallbackChannelImpl(mockChannel, this.executorService); - when(mockConnection.createChannel()).thenReturn(channel); + given(mockConnection.createChannel()).willReturn(channel); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -645,12 +645,12 @@ public class RabbitTemplatePublisherCallbacksIntegrationTests { }); Log logger = spy(TestUtils.getPropertyValue(connectionFactoryWithConfirmsEnabled, "logger", Log.class)); final AtomicReference log = new AtomicReference(); - doAnswer(invocation -> { + willAnswer(invocation -> { log.set((String) invocation.getArguments()[0]); invocation.callRealMethod(); latch.countDown(); return null; - }).when(logger).error(any()); + }).given(logger).error(any()); new DirectFieldAccessor(connectionFactoryWithConfirmsEnabled).setPropertyValue("logger", logger); CorrelationData correlationData = new CorrelationData("bar"); @@ -688,13 +688,13 @@ public class RabbitTemplatePublisherCallbacksIntegrationTests { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); - when(mockChannel.isOpen()).thenReturn(true); + given(mockChannel.isOpen()).willReturn(true); final AtomicLong seq = new AtomicLong(); - doAnswer(invocation -> seq.incrementAndGet()).when(mockChannel).getNextPublishSeqNo(); + willAnswer(invocation -> seq.incrementAndGet()).given(mockChannel).getNextPublishSeqNo(); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); - doReturn(mockChannel).when(mockConnection).createChannel(); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); + willReturn(mockChannel).given(mockConnection).createChannel(); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -747,16 +747,16 @@ public class RabbitTemplatePublisherCallbacksIntegrationTests { Connection mockConnection = mock(Connection.class); Channel mockChannel1 = mock(Channel.class); final AtomicLong seq1 = new AtomicLong(); - doAnswer(invocation -> seq1.incrementAndGet()).when(mockChannel1).getNextPublishSeqNo(); + willAnswer(invocation -> seq1.incrementAndGet()).given(mockChannel1).getNextPublishSeqNo(); Channel mockChannel2 = mock(Channel.class); - when(mockChannel2.isOpen()).thenReturn(true); + given(mockChannel2.isOpen()).willReturn(true); final AtomicLong seq2 = new AtomicLong(); - doAnswer(invocation -> seq2.incrementAndGet()).when(mockChannel2).getNextPublishSeqNo(); + willAnswer(invocation -> seq2.incrementAndGet()).given(mockChannel2).getNextPublishSeqNo(); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).thenReturn(mockConnection); - when(mockConnection.isOpen()).thenReturn(true); - when(mockConnection.createChannel()).thenReturn(mockChannel1, mockChannel2); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); + given(mockConnection.isOpen()).willReturn(true); + given(mockConnection.createChannel()).willReturn(mockChannel1, mockChannel2); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(Executors.newSingleThreadExecutor()); @@ -767,7 +767,7 @@ public class RabbitTemplatePublisherCallbacksIntegrationTests { template.setConfirmCallback((correlationData, ack, cause) -> confirmed.countDown()); ExecutorService exec = Executors.newSingleThreadExecutor(); final AtomicInteger sent = new AtomicInteger(); - doAnswer(invocation -> sent.incrementAndGet() < closeAfter).when(mockChannel1).isOpen(); + willAnswer(invocation -> sent.incrementAndGet() < closeAfter).given(mockChannel1).isOpen(); final CountDownLatch sentAll = new CountDownLatch(1); exec.execute(() -> { template.invoke(t -> { @@ -794,15 +794,15 @@ public class RabbitTemplatePublisherCallbacksIntegrationTests { Listener listener = mock(Listener.class); final CountDownLatch latch = new CountDownLatch(2); - doAnswer(invocation -> { + willAnswer(invocation -> { boolean ack = invocation.getArgument(1); if (!ack) { latch.countDown(); } return null; - }).when(listener).handleConfirm(any(PendingConfirm.class), anyBoolean()); - when(listener.getUUID()).thenReturn(UUID.randomUUID().toString()); - when(listener.isConfirmListener()).thenReturn(true); + }).given(listener).handleConfirm(any(PendingConfirm.class), anyBoolean()); + given(listener.getUUID()).willReturn(UUID.randomUUID().toString()); + given(listener.isConfirmListener()).willReturn(true); Channel channelMock = mock(Channel.class); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/BlockingQueueConsumerTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/BlockingQueueConsumerTests.java index b7983ac5..cd338855 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/BlockingQueueConsumerTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/BlockingQueueConsumerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,13 +24,11 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.willAnswer; +import static org.mockito.BDDMockito.willReturn; import static org.mockito.BDDMockito.willThrow; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.io.IOException; import java.util.HashSet; @@ -138,11 +136,11 @@ public class BlockingQueueConsumerTests { Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); - when(connectionFactory.createConnection()).thenReturn(connection); - when(connection.createChannel(anyBoolean())).thenReturn(channel); - when(channel.isOpen()).thenReturn(true); - when(channel.queueDeclarePassive(anyString())) - .then(invocation -> { + given(connectionFactory.createConnection()).willReturn(connection); + given(connection.createChannel(anyBoolean())).willReturn(channel); + given(channel.isOpen()).willReturn(true); + given(channel.queueDeclarePassive(anyString())) + .willAnswer(invocation -> { String arg = invocation.getArgument(0); if ("good".equals(arg)) { return any(AMQP.Queue.DeclareOk.class); @@ -151,8 +149,8 @@ public class BlockingQueueConsumerTests { throw new IOException(); } }); - when(channel.basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), - anyMap(), any(Consumer.class))).thenReturn("consumerTag"); + given(channel.basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), + anyMap(), any(Consumer.class))).willReturn("consumerTag"); BlockingQueueConsumer blockingQueueConsumer = new BlockingQueueConsumer(connectionFactory, new DefaultMessagePropertiesConverter(), new ActiveObjectCounter(), @@ -172,9 +170,9 @@ public class BlockingQueueConsumerTests { Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); - when(connectionFactory.createConnection()).thenReturn(connection); - when(connection.createChannel(anyBoolean())).thenReturn(channel); - when(channel.isOpen()).thenReturn(true); + given(connectionFactory.createConnection()).willReturn(connection); + given(connection.createChannel(anyBoolean())).willReturn(channel); + given(channel.isOpen()).willReturn(true); final String queue = "testQ"; final boolean noLocal = true; @@ -270,18 +268,18 @@ public class BlockingQueueConsumerTests { Connection connection = mock(Connection.class); ChannelProxy channel = mock(ChannelProxy.class); Channel rabbitChannel = mock(AutorecoveringChannel.class); - when(channel.getTargetChannel()).thenReturn(rabbitChannel); + given(channel.getTargetChannel()).willReturn(rabbitChannel); - when(connectionFactory.createConnection()).thenReturn(connection); - when(connection.createChannel(anyBoolean())).thenReturn(channel); + given(connectionFactory.createConnection()).willReturn(connection); + given(connection.createChannel(anyBoolean())).willReturn(channel); final AtomicBoolean isOpen = new AtomicBoolean(true); - doReturn(isOpen.get()).when(channel).isOpen(); - when(channel.queueDeclarePassive(anyString())) - .then(invocation -> mock(AMQP.Queue.DeclareOk.class)); - doAnswer(i -> { + willReturn(isOpen.get()).given(channel).isOpen(); + given(channel.queueDeclarePassive(anyString())) + .willAnswer(invocation -> mock(AMQP.Queue.DeclareOk.class)); + willAnswer(i -> { ((Consumer) i.getArgument(6)).handleConsumeOk("consumerTag"); return "consumerTag"; - }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), + }).given(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); BlockingQueueConsumer blockingQueueConsumer = new BlockingQueueConsumer(connectionFactory, @@ -306,26 +304,26 @@ public class BlockingQueueConsumerTests { Connection connection = mock(Connection.class); ChannelProxy channel = mock(ChannelProxy.class); Channel rabbitChannel = mock(AutorecoveringChannel.class); - when(channel.getTargetChannel()).thenReturn(rabbitChannel); + given(channel.getTargetChannel()).willReturn(rabbitChannel); - when(connectionFactory.createConnection()).thenReturn(connection); - when(connection.createChannel(anyBoolean())).thenReturn(channel); + given(connectionFactory.createConnection()).willReturn(connection); + given(connection.createChannel(anyBoolean())).willReturn(channel); final AtomicBoolean isOpen = new AtomicBoolean(true); - doReturn(isOpen.get()).when(channel).isOpen(); - when(channel.queueDeclarePassive(anyString())) - .then(invocation -> mock(AMQP.Queue.DeclareOk.class)); + willReturn(isOpen.get()).given(channel).isOpen(); + given(channel.queueDeclarePassive(anyString())) + .willAnswer(invocation -> mock(AMQP.Queue.DeclareOk.class)); AtomicReference theConsumer = new AtomicReference<>(); - doAnswer(inv -> { + willAnswer(inv -> { Consumer consumer = inv.getArgument(6); consumer.handleConsumeOk("consumerTag"); theConsumer.set(consumer); return "consumerTag"; - }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), + }).given(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); - doAnswer(inv -> { + willAnswer(inv -> { theConsumer.get().handleCancelOk("consumerTag"); return null; - }).when(channel).basicCancel("consumerTag"); + }).given(channel).basicCancel("consumerTag"); BlockingQueueConsumer blockingQueueConsumer = new BlockingQueueConsumer(connectionFactory, new DefaultMessagePropertiesConverter(), new ActiveObjectCounter(), AcknowledgeMode.AUTO, true, 2, "test"); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/ListenFromAutoDeleteQueueTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/ListenFromAutoDeleteQueueTests.java index 5aa8cc8a..88be38d8 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/ListenFromAutoDeleteQueueTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/ListenFromAutoDeleteQueueTests.java @@ -18,14 +18,14 @@ package org.springframework.amqp.rabbit.listener; import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willThrow; import static org.mockito.Mockito.atLeastOnce; -import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; @@ -137,9 +137,9 @@ public class ListenFromAutoDeleteQueueTests { //Prevent a long 'passiveDeclare' process BlockingQueueConsumer consumer = mock(BlockingQueueConsumer.class); - doThrow(RuntimeException.class).when(consumer).start(); -// when(consumer.getBackOffExecution()).thenReturn(mock(BackOffExecution.class)); - when(listenerContainer.createBlockingQueueConsumer()).thenReturn(consumer); + willThrow(RuntimeException.class).given(consumer).start(); +// given(consumer.getBackOffExecution()).willReturn(mock(BackOffExecution.class)); + given(listenerContainer.createBlockingQueueConsumer()).willReturn(consumer); listenerContainer.start(); listenerContainer.stop(); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerErrorHandlerIntegrationTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerErrorHandlerIntegrationTests.java index 5a994997..627c8a76 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerErrorHandlerIntegrationTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerErrorHandlerIntegrationTests.java @@ -21,8 +21,8 @@ import static org.assertj.core.api.Assertions.fail; import static org.awaitility.Awaitility.await; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.contains; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.doReturn; +import static org.mockito.BDDMockito.willAnswer; +import static org.mockito.BDDMockito.willReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; @@ -90,10 +90,10 @@ public class MessageListenerContainerErrorHandlerIntegrationTests { @BeforeEach public void setUp() { - doAnswer(invocation -> { + willAnswer(invocation -> { errorsHandled.countDown(); return null; - }).when(errorHandler).handleError(any(Throwable.class)); + }).given(errorHandler).handleError(any(Throwable.class)); } @Test // AMQP-385 @@ -121,14 +121,14 @@ public class MessageListenerContainerErrorHandlerIntegrationTests { container.setReceiveTimeout(50); container.start(); Log logger = spy(TestUtils.getPropertyValue(container, "logger", Log.class)); - doReturn(true).when(logger).isWarnEnabled(); + willReturn(true).given(logger).isWarnEnabled(); new DirectFieldAccessor(container).setPropertyValue("logger", logger); template.convertAndSend(QUEUE.getName(), "baz"); assertThat(messageReceived.await(10, TimeUnit.SECONDS)).isTrue(); Object consumer = TestUtils.getPropertyValue(container, "consumers", Set.class) .iterator().next(); Log qLogger = spy(TestUtils.getPropertyValue(consumer, "logger", Log.class)); - doReturn(true).when(qLogger).isDebugEnabled(); + willReturn(true).given(qLogger).isDebugEnabled(); new DirectFieldAccessor(consumer).setPropertyValue("logger", qLogger); spiedQLogger.countDown(); assertThat(errorHandled.await(10, TimeUnit.SECONDS)).isTrue(); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerLifecycleIntegrationTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerLifecycleIntegrationTests.java index e3ec0683..6aeb1ea4 100755 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerLifecycleIntegrationTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerLifecycleIntegrationTests.java @@ -19,9 +19,9 @@ package org.springframework.amqp.rabbit.listener; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.awaitility.Awaitility.await; -import static org.mockito.Mockito.doAnswer; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willAnswer; import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; import java.net.UnknownHostException; import java.util.Set; @@ -401,12 +401,12 @@ public class MessageListenerContainerLifecycleIntegrationTests { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); Log log = spy(TestUtils.getPropertyValue(container, "logger", Log.class)); final CountDownLatch latch = new CountDownLatch(1); - when(log.isDebugEnabled()).thenReturn(true); - doAnswer(invocation -> { + given(log.isDebugEnabled()).willReturn(true); + willAnswer(invocation -> { latch.countDown(); invocation.callRealMethod(); return null; - }).when(log).debug( + }).given(log).debug( Mockito.contains("Consumer received Shutdown Signal, processing stopped")); DirectFieldAccessor dfa = new DirectFieldAccessor(container); dfa.setPropertyValue("logger", log); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainerIntegration2Tests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainerIntegration2Tests.java index de7441a0..87001f5d 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainerIntegration2Tests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainerIntegration2Tests.java @@ -21,11 +21,11 @@ import static org.awaitility.Awaitility.await; import static org.awaitility.Awaitility.with; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willReturn; import static org.mockito.Mockito.atLeastOnce; -import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.io.IOException; import java.io.Serializable; @@ -333,7 +333,7 @@ public class SimpleMessageListenerContainerIntegration2Tests { @Test public void testExclusive() throws Exception { Log logger = spy(TestUtils.getPropertyValue(this.template.getConnectionFactory(), "logger", Log.class)); - doReturn(true).when(logger).isInfoEnabled(); + willReturn(true).given(logger).isInfoEnabled(); new DirectFieldAccessor(this.template.getConnectionFactory()).setPropertyValue("logger", logger); CountDownLatch latch1 = new CountDownLatch(1000); SimpleMessageListenerContainer container1 = @@ -374,7 +374,7 @@ public class SimpleMessageListenerContainerIntegration2Tests { }); container2.afterPropertiesSet(); Log containerLogger = spy(TestUtils.getPropertyValue(container2, "logger", Log.class)); - doReturn(true).when(containerLogger).isWarnEnabled(); + willReturn(true).given(containerLogger).isWarnEnabled(); new DirectFieldAccessor(container2).setPropertyValue("logger", containerLogger); container2.start(); for (int i = 0; i < 1000; i++) { @@ -430,8 +430,8 @@ public class SimpleMessageListenerContainerIntegration2Tests { } Connection connection = spy(connectionFactory.createConnection()); - when(connection.createChannel(anyBoolean())) - .then(invocation -> new MockChannel((Channel) invocation.callRealMethod())); + given(connection.createChannel(anyBoolean())) + .willAnswer(invocation -> new MockChannel((Channel) invocation.callRealMethod())); DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory); dfa.setPropertyValue("connection", connection); @@ -480,8 +480,8 @@ public class SimpleMessageListenerContainerIntegration2Tests { } Connection connection = spy(connectionFactory.createConnection()); - when(connection.createChannel(anyBoolean())) - .then(invocation -> new MockChannel((Channel) invocation.callRealMethod())); + given(connection.createChannel(anyBoolean())) + .willAnswer(invocation -> new MockChannel((Channel) invocation.callRealMethod())); DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory); dfa.setPropertyValue("connection", connection); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainerTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainerTests.java index 4350780e..2cddfeec 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainerTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainerTests.java @@ -27,15 +27,14 @@ import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyMap; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willAnswer; +import static org.mockito.BDDMockito.willReturn; +import static org.mockito.BDDMockito.willThrow; import static org.mockito.Mockito.atLeastOnce; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.io.IOException; import java.net.URL; @@ -183,21 +182,21 @@ public class SimpleMessageListenerContainerTests { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); - when(connectionFactory.createConnection()).thenReturn(connection); - when(connection.createChannel(false)).thenReturn(channel); + given(connectionFactory.createConnection()).willReturn(connection); + given(connection.createChannel(false)).willReturn(channel); final AtomicReference consumer = new AtomicReference<>(); - doAnswer(invocation -> { + willAnswer(invocation -> { consumer.set(invocation.getArgument(6)); consumer.get().handleConsumeOk("1"); return "1"; - }).when(channel) + }).given(channel) .basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); final CountDownLatch latch = new CountDownLatch(2); - doAnswer(invocation -> { + willAnswer(invocation -> { latch.countDown(); return null; - }).when(channel).basicAck(anyLong(), anyBoolean()); + }).given(channel).basicAck(anyLong(), anyBoolean()); final List messages = new ArrayList<>(); final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); @@ -234,22 +233,22 @@ public class SimpleMessageListenerContainerTests { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); - when(connectionFactory.createConnection()).thenReturn(connection); - when(connection.createChannel(false)).thenReturn(channel); + given(connectionFactory.createConnection()).willReturn(connection); + given(connection.createChannel(false)).willReturn(channel); final AtomicReference consumer = new AtomicReference<>(); final String consumerTag = "1"; - doAnswer(invocation -> { + willAnswer(invocation -> { consumer.set(invocation.getArgument(6)); consumer.get().handleConsumeOk(consumerTag); return consumerTag; - }).when(channel) + }).given(channel) .basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); final CountDownLatch latch = new CountDownLatch(2); - doAnswer(invocation -> { + willAnswer(invocation -> { latch.countDown(); return null; - }).when(channel).basicAck(anyLong(), anyBoolean()); + }).given(channel).basicAck(anyLong(), anyBoolean()); final List messages = new ArrayList<>(); final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); @@ -284,16 +283,16 @@ public class SimpleMessageListenerContainerTests { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); - when(connectionFactory.createConnection()).thenReturn(connection); - when(connection.createChannel(false)).thenReturn(channel); + given(connectionFactory.createConnection()).willReturn(connection); + given(connection.createChannel(false)).willReturn(channel); final AtomicReference consumer = new AtomicReference<>(); final AtomicReference> args = new AtomicReference<>(); - doAnswer(invocation -> { + willAnswer(invocation -> { consumer.set(invocation.getArgument(6)); consumer.get().handleConsumeOk("foo"); args.set(invocation.getArgument(5)); return "foo"; - }).when(channel) + }).given(channel) .basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), any(Map.class), any(Consumer.class)); @@ -319,10 +318,10 @@ public class SimpleMessageListenerContainerTests { Connection connection = mock(Connection.class); Channel channel1 = mock(Channel.class); Channel channel2 = mock(Channel.class); - when(channel1.isOpen()).thenReturn(true); - when(channel2.isOpen()).thenReturn(true); - when(connectionFactory.createConnection()).thenReturn(connection); - when(connection.createChannel(false)).thenReturn(channel1, channel2); + given(channel1.isOpen()).willReturn(true); + given(channel2.isOpen()).willReturn(true); + given(connectionFactory.createConnection()).willReturn(connection); + given(connection.createChannel(false)).willReturn(channel1, channel2); List consumers = new ArrayList<>(); AtomicInteger consumerTag = new AtomicInteger(); CountDownLatch latch1 = new CountDownLatch(1); @@ -367,16 +366,16 @@ public class SimpleMessageListenerContainerTests { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel1 = mock(Channel.class); - when(channel1.isOpen()).thenReturn(true); - when(connectionFactory.createConnection()).thenReturn(connection); - when(connection.createChannel(false)).thenReturn(channel1); + given(channel1.isOpen()).willReturn(true); + given(connectionFactory.createConnection()).willReturn(connection); + given(connection.createChannel(false)).willReturn(channel1); final AtomicInteger count = new AtomicInteger(); - doAnswer(invocation -> { + willAnswer(invocation -> { Consumer cons = invocation.getArgument(6); String consumerTag = "consFoo" + count.incrementAndGet(); cons.handleConsumeOk(consumerTag); return consumerTag; - }).when(channel1) + }).given(channel1) .basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); @@ -397,24 +396,24 @@ public class SimpleMessageListenerContainerTests { protected void setupMockConsume(Channel channel, final List consumers, final AtomicInteger consumerTag, final CountDownLatch latch) throws IOException { - doAnswer(invocation -> { + willAnswer(invocation -> { Consumer cons = invocation.getArgument(6); consumers.add(cons); String actualTag = String.valueOf(consumerTag.getAndIncrement()); cons.handleConsumeOk(actualTag); latch.countDown(); return actualTag; - }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), + }).given(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); } protected void setUpMockCancel(Channel channel, final List consumers) throws IOException { final Executor exec = Executors.newCachedThreadPool(); - doAnswer(invocation -> { + willAnswer(invocation -> { final String consTag = invocation.getArgument(0); exec.execute(() -> consumers.get(Integer.parseInt(consTag)).handleCancelOk(consTag)); return null; - }).when(channel).basicCancel(anyString()); + }).given(channel).basicCancel(anyString()); } @Test @@ -426,16 +425,16 @@ public class SimpleMessageListenerContainerTests { Channel mockChannel1 = mock(Channel.class); Channel mockChannel2 = mock(Channel.class); - when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())) - .thenReturn(mockConnection1) - .thenReturn(mockConnection2) - .thenReturn(null); - when(mockConnection1.createChannel()).thenReturn(mockChannel1).thenReturn(null); - when(mockConnection2.createChannel()).thenReturn(mockChannel2).thenReturn(null); - when(mockChannel1.isOpen()).thenReturn(true); - when(mockConnection1.isOpen()).thenReturn(true); - when(mockChannel2.isOpen()).thenReturn(true); - when(mockConnection2.isOpen()).thenReturn(true); + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())) + .willReturn(mockConnection1) + .willReturn(mockConnection2) + .willReturn(null); + given(mockConnection1.createChannel()).willReturn(mockChannel1).willReturn(null); + given(mockConnection2.createChannel()).willReturn(mockChannel2).willReturn(null); + given(mockChannel1.isOpen()).willReturn(true); + given(mockConnection1.isOpen()).willReturn(true); + given(mockChannel2.isOpen()).willReturn(true); + given(mockConnection2.isOpen()).willReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); @@ -448,14 +447,14 @@ public class SimpleMessageListenerContainerTests { CountDownLatch latch1 = new CountDownLatch(2); CountDownLatch latch2 = new CountDownLatch(2); - doAnswer(messageToConsumer(mockChannel1, container, false, latch1)) - .when(mockChannel1).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), + willAnswer(messageToConsumer(mockChannel1, container, false, latch1)) + .given(mockChannel1).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); - doAnswer(messageToConsumer(mockChannel2, container, false, latch1)) - .when(mockChannel2).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), + willAnswer(messageToConsumer(mockChannel2, container, false, latch1)) + .given(mockChannel2).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); - doAnswer(messageToConsumer(mockChannel1, container, true, latch2)).when(mockChannel1).basicCancel(anyString()); - doAnswer(messageToConsumer(mockChannel2, container, true, latch2)).when(mockChannel2).basicCancel(anyString()); + willAnswer(messageToConsumer(mockChannel1, container, true, latch2)).given(mockChannel1).basicCancel(anyString()); + willAnswer(messageToConsumer(mockChannel2, container, true, latch2)).given(mockChannel2).basicCancel(anyString()); container.start(); assertThat(latch1.await(10, TimeUnit.SECONDS)).isTrue(); @@ -474,14 +473,14 @@ public class SimpleMessageListenerContainerTests { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); - when(connectionFactory.createConnection()).thenReturn(connection); - when(connection.createChannel(false)).thenReturn(channel); + given(connectionFactory.createConnection()).willReturn(connection); + given(connection.createChannel(false)).willReturn(channel); final AtomicReference consumer = new AtomicReference<>(); - doAnswer(invocation -> { + willAnswer(invocation -> { consumer.set(invocation.getArgument(6)); consumer.get().handleConsumeOk("foo"); return "foo"; - }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), + }).given(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); @@ -494,18 +493,18 @@ public class SimpleMessageListenerContainerTests { verify(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); Log logger = spy(TestUtils.getPropertyValue(container, "logger", Log.class)); - doReturn(false).when(logger).isDebugEnabled(); - doReturn(true).when(logger).isWarnEnabled(); + willReturn(false).given(logger).isDebugEnabled(); + willReturn(true).given(logger).isWarnEnabled(); final CountDownLatch latch = new CountDownLatch(1); final List messages = new ArrayList<>(); - doAnswer(invocation -> { + willAnswer(invocation -> { String message = invocation.getArgument(0); messages.add(message); if (message.startsWith("Consumer raised exception")) { latch.countDown(); } return invocation.callRealMethod(); - }).when(logger).warn(any()); + }).given(logger).warn(any()); new DirectFieldAccessor(container).setPropertyValue("logger", logger); consumer.get().handleCancel("foo"); assertThat(latch.await(10, TimeUnit.SECONDS)) @@ -521,11 +520,11 @@ public class SimpleMessageListenerContainerTests { container.setQueueNames("foo"); container.setRecoveryBackOff(new FixedBackOff(100, 3)); container.setConcurrentConsumers(3); - doAnswer(invocation -> { + willAnswer(invocation -> { BlockingQueueConsumer consumer = spy((BlockingQueueConsumer) invocation.callRealMethod()); - doThrow(RuntimeException.class).when(consumer).start(); + willThrow(RuntimeException.class).given(consumer).start(); return consumer; - }).when(container).createBlockingQueueConsumer(); + }).given(container).createBlockingQueueConsumer(); container.afterPropertiesSet(); container.start(); diff --git a/src/checkstyle/checkstyle.xml b/src/checkstyle/checkstyle.xml index 73ba6197..92584d73 100644 --- a/src/checkstyle/checkstyle.xml +++ b/src/checkstyle/checkstyle.xml @@ -165,6 +165,11 @@ + + + + +