From 4ef8e963a905c7b786aad658667c43348ae1e6df Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Tue, 16 Apr 2019 18:51:31 -0400 Subject: [PATCH] Use assertThatThrownBy() Vs. ExpectedException Also avoid `access()` method in `RabbitTemplatePublisherCallbacksIntegrationTests3`. --- .../AbstractRabbitAnnotationDrivenTests.java | 5 -- .../RabbitListenerContainerFactoryTests.java | 5 -- .../SimpleRabbitListenerEndpointTests.java | 10 +-- ...hingConnectionFactoryIntegrationTests.java | 57 +++++++--------- .../amqp/rabbit/core/RabbitAdminTests.java | 8 +-- .../core/RabbitMessagingTemplateTests.java | 68 +++++++++---------- ...tePublisherCallbacksIntegrationTests3.java | 2 +- .../amqp/rabbit/core/RabbitTemplateTests.java | 14 ++-- ...ontainerMultipleQueueIntegrationTests.java | 4 -- ...istenerContainerRetryIntegrationTests.java | 6 +- .../MethodRabbitListenerEndpointTests.java | 65 +++++++++--------- .../RabbitListenerEndpointRegistrarTests.java | 25 +++---- .../RabbitListenerEndpointRegistryTests.java | 24 +++---- ...sageListenerContainerIntegrationTests.java | 13 ++-- .../SimpleMessageListenerContainerTests.java | 14 ++-- 15 files changed, 134 insertions(+), 186 deletions(-) diff --git a/spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/repeatable/AbstractRabbitAnnotationDrivenTests.java b/spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/repeatable/AbstractRabbitAnnotationDrivenTests.java index 991c6b1b..857ee983 100644 --- a/spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/repeatable/AbstractRabbitAnnotationDrivenTests.java +++ b/spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/repeatable/AbstractRabbitAnnotationDrivenTests.java @@ -18,9 +18,7 @@ package org.springframework.amqp.rabbit.repeatable; import static org.assertj.core.api.Assertions.assertThat; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; @@ -38,9 +36,6 @@ import org.springframework.stereotype.Component; */ public abstract class AbstractRabbitAnnotationDrivenTests { - @Rule - public final ExpectedException thrown = ExpectedException.none(); - @Test public abstract void rabbitListenerIsRepeatable(); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/config/RabbitListenerContainerFactoryTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/config/RabbitListenerContainerFactoryTests.java index 1e06461b..1f59e279 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/config/RabbitListenerContainerFactoryTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/config/RabbitListenerContainerFactoryTests.java @@ -24,9 +24,7 @@ import java.util.List; import java.util.concurrent.Executor; import org.aopalliance.aop.Advice; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.amqp.core.AcknowledgeMode; import org.springframework.amqp.core.MessagePostProcessor; @@ -54,9 +52,6 @@ import org.springframework.util.backoff.ExponentialBackOff; */ public class RabbitListenerContainerFactoryTests { - @Rule - public final ExpectedException thrown = ExpectedException.none(); - private final SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); private final DirectRabbitListenerContainerFactory direct = new DirectRabbitListenerContainerFactory(); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/config/SimpleRabbitListenerEndpointTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/config/SimpleRabbitListenerEndpointTests.java index 50ee94d8..400afd62 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/config/SimpleRabbitListenerEndpointTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/config/SimpleRabbitListenerEndpointTests.java @@ -18,11 +18,10 @@ package org.springframework.amqp.rabbit.config; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.mockito.Mockito.mock; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.amqp.core.MessageListener; import org.springframework.amqp.core.Queue; @@ -35,9 +34,6 @@ import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; */ public class SimpleRabbitListenerEndpointTests { - @Rule - public final ExpectedException thrown = ExpectedException.none(); - private final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); private final MessageListener messageListener = new MessageListenerAdapter(); @@ -57,8 +53,8 @@ public class SimpleRabbitListenerEndpointTests { endpoint.setQueueNames("foo", "bar"); endpoint.setQueues(mock(Queue.class)); - thrown.expect(IllegalStateException.class); - endpoint.setupListenerContainer(container); + assertThatIllegalStateException() + .isThrownBy(() -> endpoint.setupListenerContainer(container)); } } diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryIntegrationTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryIntegrationTests.java index d969403b..2ebd09f4 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryIntegrationTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryIntegrationTests.java @@ -17,9 +17,8 @@ package org.springframework.amqp.rabbit.connection; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.fail; -import static org.hamcrest.Matchers.anyOf; -import static org.hamcrest.Matchers.instanceOf; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -49,7 +48,6 @@ import org.junit.Before; import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.amqp.AmqpApplicationContextClosedException; import org.springframework.amqp.AmqpAuthenticationException; @@ -94,9 +92,6 @@ public class CachingConnectionFactoryIntegrationTests { @Rule public BrokerRunning brokerIsRunning = BrokerRunning.isRunningWithEmptyQueues(CF_INTEGRATION_TEST_QUEUE); - @Rule - public ExpectedException exception = ExpectedException.none(); - @Rule public LogLevelAdjuster adjuster = new LogLevelAdjuster(Level.DEBUG, CachingConnectionFactoryIntegrationTests.class, CachingConnectionFactory.class) @@ -270,20 +265,21 @@ public class CachingConnectionFactoryIntegrationTests { } @Test - public void testReceiveFromNonExistentVirtualHost() throws Exception { + public void testReceiveFromNonExistentVirtualHost() { connectionFactory.setVirtualHost("non-existent"); RabbitTemplate template = new RabbitTemplate(connectionFactory); - // Wrong vhost is very unfriendly to client - the exception has no clue (just an EOF) - exception.expect(anyOf(instanceOf(AmqpIOException.class), - instanceOf(AmqpAuthenticationException.class), - /* - * If localhost also resolves to an IPv6 address, the client will try that - * after a failure due to an invalid vHost and, if Rabbit is not listening there, - * we'll get an... - */ - instanceOf(AmqpConnectException.class))); - template.receiveAndConvert("foo"); + assertThatThrownBy(() -> template.receiveAndConvert("foo")) + .isInstanceOfAny( + // Wrong vhost is very unfriendly to client - the exception has no clue (just an EOF) + AmqpIOException.class, + AmqpAuthenticationException.class, + /* + * If localhost also resolves to an IPv6 address, the client will try that + * after a failure due to an invalid vHost and, if Rabbit is not listening there, + * we'll get an... + */ + AmqpConnectException.class); } @Test @@ -296,13 +292,11 @@ public class CachingConnectionFactoryIntegrationTests { template.convertAndSend(queue.getName(), "message"); // Force a physical close of the channel - connectionFactory.destroy(); + this.connectionFactory.resetConnection(); // The queue was removed when the channel was closed - exception.expect(AmqpIOException.class); - - String result = (String) template.receiveAndConvert(queue.getName()); - assertThat(result).isEqualTo("message"); + assertThatThrownBy(() -> template.receiveAndConvert(queue.getName())) + .isInstanceOf(AmqpIOException.class); template.stop(); } @@ -321,13 +315,12 @@ public class CachingConnectionFactoryIntegrationTests { assertThat(result).isEqualTo("message"); // The channel is not transactional - exception.expect(AmqpIOException.class); - - template2.execute(channel -> { - // Should be an exception because the channel is not transactional - channel.txRollback(); - return null; - }); + assertThatThrownBy(() -> + template2.execute(channel -> { + // Should be an exception because the channel is not transactional + channel.txRollback(); + return null; + })).isInstanceOf(AmqpIOException.class); } @@ -370,12 +363,12 @@ public class CachingConnectionFactoryIntegrationTests { @Test public void testConnectionCloseLog() { - Log logger = spy(TestUtils.getPropertyValue(this.connectionFactory, "logger", Log.class)); - new DirectFieldAccessor(this.connectionFactory).setPropertyValue("logger", logger); + Log log = spy(TestUtils.getPropertyValue(this.connectionFactory, "logger", Log.class)); + new DirectFieldAccessor(this.connectionFactory).setPropertyValue("logger", log); Connection conn = this.connectionFactory.createConnection(); conn.createChannel(false); this.connectionFactory.destroy(); - verify(logger, never()).error(anyString()); + verify(log, never()).error(anyString()); } @Test 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 7f04da52..93d9dde2 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 @@ -17,6 +17,7 @@ package org.springframework.amqp.rabbit.core; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.assertj.core.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; @@ -47,7 +48,6 @@ import org.apache.commons.logging.Log; import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; import org.springframework.amqp.UncategorizedAmqpException; @@ -97,9 +97,6 @@ import com.rabbitmq.http.client.domain.QueueInfo; */ public class RabbitAdminTests { - @Rule - public ExpectedException exception = ExpectedException.none(); - @Rule public BrokerRunning brokerIsRunning = BrokerRunning.isBrokerAndManagementRunning(); @@ -138,8 +135,7 @@ public class RabbitAdminTests { rabbitAdmin.setApplicationContext(applicationContext); rabbitAdmin.setAutoStartup(true); rabbitAdmin.afterPropertiesSet(); - exception.expect(IllegalArgumentException.class); - rabbitAdmin.declareQueue(); + assertThatIllegalArgumentException().isThrownBy(() -> rabbitAdmin.declareQueue()); connectionFactory.destroy(); } diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitMessagingTemplateTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitMessagingTemplateTests.java index 62dd5332..8581e1d5 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitMessagingTemplateTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitMessagingTemplateTests.java @@ -17,6 +17,7 @@ package org.springframework.amqp.rabbit.core; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -31,9 +32,7 @@ import java.util.HashMap; import java.util.Map; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; @@ -58,9 +57,6 @@ import org.springframework.messaging.support.MessageBuilder; */ public class RabbitMessagingTemplateTests { - @Rule - public final ExpectedException thrown = ExpectedException.none(); - @Captor private ArgumentCaptor amqpMessage; @@ -130,8 +126,8 @@ public class RabbitMessagingTemplateTests { public void sendNoDefaultSet() { Message message = createTextMessage(); - thrown.expect(IllegalStateException.class); - messagingTemplate.send(message); + assertThatIllegalStateException() + .isThrownBy(() -> messagingTemplate.send(message)); } @Test @@ -172,8 +168,8 @@ public class RabbitMessagingTemplateTests { @Test public void convertAndSendNoDefaultSet() { - thrown.expect(IllegalStateException.class); - messagingTemplate.convertAndSend("my Payload"); + assertThatIllegalStateException() + .isThrownBy(() -> messagingTemplate.convertAndSend("my Payload")); } @Test @@ -214,8 +210,8 @@ public class RabbitMessagingTemplateTests { @Test public void receive() { - org.springframework.amqp.core.Message amqpMessage = createAmqpTextMessage(); - given(rabbitTemplate.receive("myQueue")).willReturn(amqpMessage); + org.springframework.amqp.core.Message amqpMsg = createAmqpTextMessage(); + given(rabbitTemplate.receive("myQueue")).willReturn(amqpMsg); Message message = messagingTemplate.receive("myQueue"); verify(rabbitTemplate).receive("myQueue"); @@ -226,8 +222,8 @@ public class RabbitMessagingTemplateTests { public void receiveDefaultDestination() { messagingTemplate.setDefaultDestination("default"); - org.springframework.amqp.core.Message amqpMessage = createAmqpTextMessage(); - given(rabbitTemplate.receive("default")).willReturn(amqpMessage); + org.springframework.amqp.core.Message amqpMsg = createAmqpTextMessage(); + given(rabbitTemplate.receive("default")).willReturn(amqpMsg); Message message = messagingTemplate.receive(); verify(rabbitTemplate).receive("default"); @@ -236,14 +232,14 @@ public class RabbitMessagingTemplateTests { @Test public void receiveNoDefaultSet() { - thrown.expect(IllegalStateException.class); - messagingTemplate.receive(); + assertThatIllegalStateException() + .isThrownBy(() -> messagingTemplate.receive()); } @Test public void receiveAndConvert() { - org.springframework.amqp.core.Message amqpMessage = createAmqpTextMessage("my Payload"); - given(rabbitTemplate.receive("myQueue")).willReturn(amqpMessage); + org.springframework.amqp.core.Message amqpMsg = createAmqpTextMessage("my Payload"); + given(rabbitTemplate.receive("myQueue")).willReturn(amqpMsg); String payload = messagingTemplate.receiveAndConvert("myQueue", String.class); @@ -255,8 +251,8 @@ public class RabbitMessagingTemplateTests { public void receiveAndConvertDefaultDestination() { messagingTemplate.setDefaultDestination("default"); - org.springframework.amqp.core.Message amqpMessage = createAmqpTextMessage("my Payload"); - given(rabbitTemplate.receive("default")).willReturn(amqpMessage); + org.springframework.amqp.core.Message amqpMsg = createAmqpTextMessage("my Payload"); + given(rabbitTemplate.receive("default")).willReturn(amqpMsg); String payload = messagingTemplate.receiveAndConvert(String.class); @@ -266,8 +262,8 @@ public class RabbitMessagingTemplateTests { @Test public void receiveAndConvertWithConversion() { - org.springframework.amqp.core.Message amqpMessage = createAmqpTextMessage("123"); - given(rabbitTemplate.receive("myQueue")).willReturn(amqpMessage); + org.springframework.amqp.core.Message message = createAmqpTextMessage("123"); + given(rabbitTemplate.receive("myQueue")).willReturn(message); messagingTemplate.setMessageConverter(new GenericMessageConverter()); @@ -278,11 +274,11 @@ public class RabbitMessagingTemplateTests { @Test public void receiveAndConvertNoConverter() { - org.springframework.amqp.core.Message amqpMessage = createAmqpTextMessage("Hello"); - given(rabbitTemplate.receive("myQueue")).willReturn(amqpMessage); + org.springframework.amqp.core.Message message = createAmqpTextMessage("Hello"); + given(rabbitTemplate.receive("myQueue")).willReturn(message); - thrown.expect(org.springframework.messaging.converter.MessageConversionException.class); - messagingTemplate.receiveAndConvert("myQueue", Writer.class); + assertThatThrownBy(() -> messagingTemplate.receiveAndConvert("myQueue", Writer.class)) + .isInstanceOf(org.springframework.messaging.converter.MessageConversionException.class); } @Test @@ -334,8 +330,8 @@ public class RabbitMessagingTemplateTests { public void sendAndReceiveNoDefaultSet() { Message message = createTextMessage(); - thrown.expect(IllegalStateException.class); - messagingTemplate.sendAndReceive(message); + assertThatIllegalStateException() + .isThrownBy(() -> messagingTemplate.sendAndReceive(message)); } @Test @@ -372,8 +368,8 @@ public class RabbitMessagingTemplateTests { @Test public void convertSendAndReceiveNoDefaultSet() { - thrown.expect(IllegalStateException.class); - messagingTemplate.convertSendAndReceive("my Payload", String.class); + assertThatIllegalStateException() + .isThrownBy(() -> messagingTemplate.convertSendAndReceive("my Payload", String.class)); } @Test @@ -384,8 +380,8 @@ public class RabbitMessagingTemplateTests { .given(messageConverter).toMessage(eq(message), anyMessageProperties()); messagingTemplate.setAmqpMessageConverter(messageConverter); - thrown.expect(org.springframework.messaging.converter.MessageConversionException.class); - messagingTemplate.send("myQueue", message); + assertThatThrownBy(() -> messagingTemplate.send("myQueue", message)) + .isInstanceOf(org.springframework.messaging.converter.MessageConversionException.class); } @Test @@ -397,8 +393,8 @@ public class RabbitMessagingTemplateTests { messagingTemplate.setAmqpMessageConverter(messageConverter); given(rabbitTemplate.receive("myQueue")).willReturn(message); - thrown.expect(org.springframework.messaging.converter.MessageConversionException.class); - messagingTemplate.receive("myQueue"); + assertThatThrownBy(() -> messagingTemplate.receive("myQueue")) + .isInstanceOf(org.springframework.messaging.converter.MessageConversionException.class); } private Message createTextMessage(String payload) { @@ -422,9 +418,9 @@ public class RabbitMessagingTemplateTests { return createAmqpTextMessage("Hello"); } - private void assertTextMessage(org.springframework.amqp.core.Message amqpMessage) { - assertThat(MessageTestUtils.extractText(amqpMessage)).as("Wrong body message").isEqualTo("Hello"); - assertThat(amqpMessage.getMessageProperties().getHeaders().get("foo")).as("Invalid foo property").isEqualTo("bar"); + private void assertTextMessage(org.springframework.amqp.core.Message amqpMsg) { + assertThat(MessageTestUtils.extractText(amqpMsg)).as("Wrong body message").isEqualTo("Hello"); + assertThat(amqpMsg.getMessageProperties().getHeaders().get("foo")).as("Invalid foo property").isEqualTo("bar"); } private void assertTextMessage(Message message) { diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplatePublisherCallbacksIntegrationTests3.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplatePublisherCallbacksIntegrationTests3.java index 2588145d..ea72e039 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplatePublisherCallbacksIntegrationTests3.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplatePublisherCallbacksIntegrationTests3.java @@ -147,7 +147,7 @@ public class RabbitTemplatePublisherCallbacksIntegrationTests3 { private static class MyCD extends CorrelationData { - private final String payload; + final String payload; MyCD(String payload) { this.payload = payload; diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateTests.java index b1aa1d2a..6e43ae15 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateTests.java @@ -17,6 +17,7 @@ package org.springframework.amqp.rabbit.core; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.assertj.core.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; @@ -42,9 +43,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.mockito.Mockito; import org.springframework.amqp.AmqpAuthenticationException; @@ -95,9 +94,6 @@ import com.rabbitmq.client.impl.AMQImpl.Queue.DeclareOk; */ public class RabbitTemplateTests { - @Rule - public ExpectedException exception = ExpectedException.none(); - @Test public void returnConnectionAfterCommit() throws Exception { @SuppressWarnings("serial") @@ -275,16 +271,16 @@ public class RabbitTemplateTests { @Test public void testNoListenerAllowed1() { RabbitTemplate template = new RabbitTemplate(); - this.exception.expect(IllegalStateException.class); - template.expectedQueueNames(); + assertThatIllegalStateException() + .isThrownBy(() -> template.expectedQueueNames()); } @Test public void testNoListenerAllowed2() { RabbitTemplate template = new RabbitTemplate(); template.setReplyAddress(Address.AMQ_RABBITMQ_REPLY_TO); - this.exception.expect(IllegalStateException.class); - template.expectedQueueNames(); + assertThatIllegalStateException() + .isThrownBy(() -> template.expectedQueueNames()); } public final static AtomicInteger LOOKUP_KEY_COUNT = new AtomicInteger(); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerMultipleQueueIntegrationTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerMultipleQueueIntegrationTests.java index 3d5e39af..14a78257 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerMultipleQueueIntegrationTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerMultipleQueueIntegrationTests.java @@ -28,7 +28,6 @@ import org.apache.logging.log4j.Level; import org.junit.After; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.amqp.core.AcknowledgeMode; import org.springframework.amqp.core.Queue; @@ -60,9 +59,6 @@ public class MessageListenerContainerMultipleQueueIntegrationTests { public LogLevelAdjuster logLevels = new LogLevelAdjuster(Level.INFO, RabbitTemplate.class, SimpleMessageListenerContainer.class, BlockingQueueConsumer.class); - @Rule - public ExpectedException exception = ExpectedException.none(); - @After public void tearDown() { this.brokerIsRunning.removeTestQueues(); diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerRetryIntegrationTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerRetryIntegrationTests.java index 99e7dc19..a8234240 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerRetryIntegrationTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerRetryIntegrationTests.java @@ -31,7 +31,6 @@ import org.apache.logging.log4j.Level; import org.junit.After; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.amqp.core.AcknowledgeMode; import org.springframework.amqp.core.Queue; @@ -79,9 +78,6 @@ public class MessageListenerContainerRetryIntegrationTests { public LogLevelAdjuster traceLevels = new LogLevelAdjuster(Level.ERROR, StatefulRetryOperationsInterceptorFactoryBean.class, MessageListenerContainerRetryIntegrationTests.class); - @Rule - public ExpectedException exception = ExpectedException.none(); - @Rule public RepeatProcessor repeats = new RepeatProcessor(); @@ -135,7 +131,7 @@ public class MessageListenerContainerRetryIntegrationTests { } @Test - public void testStatefulRetryWithNoMessageIds() throws Exception { + public void testStatefulRetryWithNoMessageIds() { int messageCount = 2; int txSize = 1; diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MethodRabbitListenerEndpointTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MethodRabbitListenerEndpointTests.java index a7f27f7e..0f5628a5 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MethodRabbitListenerEndpointTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MethodRabbitListenerEndpointTests.java @@ -17,6 +17,8 @@ package org.springframework.amqp.rabbit.listener; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.fail; import static org.mockito.AdditionalMatchers.aryEq; import static org.mockito.ArgumentMatchers.eq; @@ -29,11 +31,9 @@ import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.rules.TestName; import org.mockito.ArgumentCaptor; @@ -75,9 +75,6 @@ public class MethodRabbitListenerEndpointTests { @Rule public final TestName name = new TestName(); - @Rule - public final ExpectedException thrown = ExpectedException.none(); - private final DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory(); private final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); @@ -96,8 +93,8 @@ public class MethodRabbitListenerEndpointTests { endpoint.setBean(this); endpoint.setMethod(getTestMethod()); - thrown.expect(IllegalStateException.class); - endpoint.createMessageListener(container); + assertThatIllegalStateException() + .isThrownBy(() -> endpoint.createMessageListener(container)); } @Test @@ -333,10 +330,9 @@ public class MethodRabbitListenerEndpointTests { @Test public void invalidSendTo() { - thrown.expect(IllegalStateException.class); - thrown.expectMessage("firstDestination"); - thrown.expectMessage("secondDestination"); - createDefaultInstance(String.class); + assertThatIllegalStateException() + .isThrownBy(() -> createDefaultInstance(String.class)) + .withMessageMatching(".*firstDestination, secondDestination.*"); } @Test @@ -355,7 +351,7 @@ public class MethodRabbitListenerEndpointTests { } @Test - public void validatePayloadInvalid() throws Exception { + public void validatePayloadInvalid() { DefaultMessageHandlerMethodFactory customFactory = new DefaultMessageHandlerMethodFactory(); customFactory.setValidator(testValidator("invalid value")); @@ -363,47 +359,52 @@ public class MethodRabbitListenerEndpointTests { MessagingMessageListenerAdapter listener = createInstance(customFactory, method); Channel channel = mock(Channel.class); - thrown.expect(ListenerExecutionFailedException.class); - listener.onMessage(MessageTestUtils.createTextMessage("invalid value"), channel); // test is an invalid value + assertThatThrownBy(() -> listener.onMessage(MessageTestUtils.createTextMessage("invalid value"), channel)) + .isInstanceOf(ListenerExecutionFailedException.class); } // failure scenario @Test - public void invalidPayloadType() throws Exception { + public void invalidPayloadType() { MessagingMessageListenerAdapter listener = createDefaultInstance(Integer.class); Channel channel = mock(Channel.class); - thrown.expect(ListenerExecutionFailedException.class); - thrown.expectCause(Matchers.isA(org.springframework.messaging.converter.MessageConversionException.class)); - thrown.expectMessage(getDefaultListenerMethod(Integer.class).toGenericString()); // ref to method - listener.onMessage(MessageTestUtils.createTextMessage("test"), channel); // test is not a valid integer + // test is not a valid integer + assertThatThrownBy(() -> listener.onMessage(MessageTestUtils.createTextMessage("test"), channel)) + .isInstanceOf(ListenerExecutionFailedException.class) + .hasCauseExactlyInstanceOf(org.springframework.messaging.converter.MessageConversionException.class) + .hasMessageContaining(getDefaultListenerMethod(Integer.class).toGenericString()); // ref to method } @Test - public void invalidMessagePayloadType() throws Exception { + public void invalidMessagePayloadType() { MessagingMessageListenerAdapter listener = createDefaultInstance(Message.class); Channel channel = mock(Channel.class); - thrown.expect(ListenerExecutionFailedException.class); - thrown.expectCause(Matchers.either(Matchers.instanceOf(MethodArgumentTypeMismatchException.class)) - .or(Matchers.instanceOf(org.springframework.messaging.converter.MessageConversionException.class))); - listener.onMessage(MessageTestUtils.createTextMessage("test"), channel); // Message as Message + // Message as Message + assertThatThrownBy(() -> listener.onMessage(MessageTestUtils.createTextMessage("test"), channel)) + .extracting(t -> t.getCause()) + .isInstanceOfAny(MethodArgumentTypeMismatchException.class, + org.springframework.messaging.converter.MessageConversionException.class); } private MessagingMessageListenerAdapter createInstance( - DefaultMessageHandlerMethodFactory factory, Method method, MessageListenerContainer container) { + DefaultMessageHandlerMethodFactory methodFactory, Method method, + MessageListenerContainer listenerContainer) { + MethodRabbitListenerEndpoint endpoint = new MethodRabbitListenerEndpoint(); endpoint.setBean(sample); endpoint.setMethod(method); - endpoint.setMessageHandlerMethodFactory(factory); - return endpoint.createMessageListener(container); + endpoint.setMessageHandlerMethodFactory(methodFactory); + return endpoint.createMessageListener(listenerContainer); } private MessagingMessageListenerAdapter createInstance( - DefaultMessageHandlerMethodFactory factory, Method method) { - return createInstance(factory, method, new SimpleMessageListenerContainer()); + DefaultMessageHandlerMethodFactory methodFactory, Method method) { + + return createInstance(methodFactory, method, new SimpleMessageListenerContainer()); } private MessagingMessageListenerAdapter createDefaultInstance(Class... parameterTypes) { @@ -428,9 +429,9 @@ public class MethodRabbitListenerEndpointTests { assertThat(bean.invocations.get(methodName)).as("Method " + methodName + " should have been invoked").isTrue(); } - private void initializeFactory(DefaultMessageHandlerMethodFactory factory) { - factory.setBeanFactory(new StaticListableBeanFactory()); - factory.afterPropertiesSet(); + private void initializeFactory(DefaultMessageHandlerMethodFactory methodFactory) { + methodFactory.setBeanFactory(new StaticListableBeanFactory()); + methodFactory.afterPropertiesSet(); } private Validator testValidator(final String invalidValue) { diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/RabbitListenerEndpointRegistrarTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/RabbitListenerEndpointRegistrarTests.java index a0f89add..d6598402 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/RabbitListenerEndpointRegistrarTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/RabbitListenerEndpointRegistrarTests.java @@ -17,11 +17,11 @@ package org.springframework.amqp.rabbit.listener; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.amqp.rabbit.config.RabbitListenerContainerTestFactory; import org.springframework.amqp.rabbit.config.SimpleRabbitListenerEndpoint; @@ -34,9 +34,6 @@ import org.springframework.beans.factory.support.StaticListableBeanFactory; */ public class RabbitListenerEndpointRegistrarTests { - @Rule - public final ExpectedException thrown = ExpectedException.none(); - private final RabbitListenerEndpointRegistrar registrar = new RabbitListenerEndpointRegistrar(); private final RabbitListenerEndpointRegistry registry = new RabbitListenerEndpointRegistry(); @@ -52,14 +49,14 @@ public class RabbitListenerEndpointRegistrarTests { @Test public void registerNullEndpoint() { - thrown.expect(IllegalArgumentException.class); - registrar.registerEndpoint(null, containerFactory); + assertThatIllegalArgumentException() + .isThrownBy(() -> registrar.registerEndpoint(null, containerFactory)); } @Test public void registerNullEndpointId() { - thrown.expect(IllegalArgumentException.class); - registrar.registerEndpoint(new SimpleRabbitListenerEndpoint(), containerFactory); + assertThatIllegalArgumentException() + .isThrownBy(() -> registrar.registerEndpoint(new SimpleRabbitListenerEndpoint(), containerFactory)); } @Test @@ -67,8 +64,8 @@ public class RabbitListenerEndpointRegistrarTests { SimpleRabbitListenerEndpoint endpoint = new SimpleRabbitListenerEndpoint(); endpoint.setId(""); - thrown.expect(IllegalArgumentException.class); - registrar.registerEndpoint(endpoint, containerFactory); + assertThatIllegalArgumentException() + .isThrownBy(() -> registrar.registerEndpoint(endpoint, containerFactory)); } @Test @@ -89,9 +86,9 @@ public class RabbitListenerEndpointRegistrarTests { endpoint.setId("some id"); registrar.registerEndpoint(endpoint, null); - thrown.expect(IllegalStateException.class); - thrown.expectMessage(endpoint.toString()); - registrar.afterPropertiesSet(); + assertThatIllegalStateException() + .isThrownBy(() -> registrar.afterPropertiesSet()) + .withMessageContaining(endpoint.toString()); } @Test diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/RabbitListenerEndpointRegistryTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/RabbitListenerEndpointRegistryTests.java index ed43d57c..2ccaf2ec 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/RabbitListenerEndpointRegistryTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/RabbitListenerEndpointRegistryTests.java @@ -16,9 +16,10 @@ package org.springframework.amqp.rabbit.listener; -import org.junit.Rule; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; + import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.amqp.rabbit.config.RabbitListenerContainerTestFactory; import org.springframework.amqp.rabbit.config.SimpleRabbitListenerEndpoint; @@ -30,37 +31,34 @@ import org.springframework.amqp.rabbit.config.SimpleRabbitListenerEndpoint; */ public class RabbitListenerEndpointRegistryTests { - @Rule - public final ExpectedException thrown = ExpectedException.none(); - private final RabbitListenerEndpointRegistry registry = new RabbitListenerEndpointRegistry(); private final RabbitListenerContainerTestFactory containerFactory = new RabbitListenerContainerTestFactory(); @Test public void createWithNullEndpoint() { - thrown.expect(IllegalArgumentException.class); - registry.registerListenerContainer(null, containerFactory); + assertThatIllegalArgumentException() + .isThrownBy(() -> registry.registerListenerContainer(null, containerFactory)); } @Test public void createWithNullEndpointId() { - thrown.expect(IllegalArgumentException.class); - registry.registerListenerContainer(new SimpleRabbitListenerEndpoint(), containerFactory); + assertThatIllegalArgumentException() + .isThrownBy(() -> registry.registerListenerContainer(new SimpleRabbitListenerEndpoint(), containerFactory)); } @Test public void createWithNullContainerFactory() { - thrown.expect(IllegalArgumentException.class); - registry.registerListenerContainer(createEndpoint("foo", "myDestination"), null); + assertThatIllegalArgumentException() + .isThrownBy(() -> registry.registerListenerContainer(createEndpoint("foo", "myDestination"), null)); } @Test public void createWithDuplicateEndpointId() { registry.registerListenerContainer(createEndpoint("test", "queue"), containerFactory); - thrown.expect(IllegalStateException.class); - registry.registerListenerContainer(createEndpoint("test", "queue"), containerFactory); + assertThatIllegalStateException() + .isThrownBy(() -> registry.registerListenerContainer(createEndpoint("test", "queue"), containerFactory)); } private SimpleRabbitListenerEndpoint createEndpoint(String id, String queueName) { diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainerIntegrationTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainerIntegrationTests.java index 0f593730..993f9357 100755 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainerIntegrationTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainerIntegrationTests.java @@ -17,6 +17,7 @@ package org.springframework.amqp.rabbit.listener; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import java.util.Arrays; import java.util.List; @@ -31,7 +32,6 @@ import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @@ -91,9 +91,6 @@ public class SimpleMessageListenerContainerIntegrationTests { @Rule public BrokerRunning brokerIsRunning = BrokerRunning.isRunningWithEmptyQueues(queue.getName()); - @Rule - public ExpectedException exception = ExpectedException.none(); - private final int messageCount; private SimpleMessageListenerContainer container; @@ -214,14 +211,14 @@ public class SimpleMessageListenerContainerIntegrationTests { @Test public void testNullQueue() { - exception.expect(IllegalArgumentException.class); - container = createContainer(m -> { }, (Queue) null); + assertThatIllegalArgumentException() + .isThrownBy(() -> container = createContainer(m -> { }, (Queue) null)); } @Test public void testNullQueueName() { - exception.expect(IllegalArgumentException.class); - container = createContainer(m -> { }, (String) null); + assertThatIllegalArgumentException() + .isThrownBy(() -> container = createContainer(m -> { }, (String) null)); } private void doSunnyDayTest(CountDownLatch latch, MessageListener listener) throws Exception { 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 a884d2d9..51f6fb2f 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 @@ -17,6 +17,7 @@ package org.springframework.amqp.rabbit.listener; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.assertj.core.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; @@ -54,9 +55,7 @@ import java.util.concurrent.atomic.AtomicReference; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.logging.Log; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.mockito.stubbing.Answer; import org.springframework.amqp.AmqpAuthenticationException; @@ -101,9 +100,6 @@ import com.rabbitmq.client.PossibleAuthenticationFailureException; */ public class SimpleMessageListenerContainerTests { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void testChannelTransactedOverriddenWhenTxManager() { final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost"); @@ -127,8 +123,8 @@ public class SimpleMessageListenerContainerTests { container.setChannelTransacted(false); container.setAcknowledgeMode(AcknowledgeMode.NONE); container.setTransactionManager(new TestTransactionManager()); - expectedException.expect(IllegalStateException.class); - container.afterPropertiesSet(); + assertThatIllegalStateException() + .isThrownBy(() -> container.afterPropertiesSet()); container.stop(); singleConnectionFactory.destroy(); } @@ -141,8 +137,8 @@ public class SimpleMessageListenerContainerTests { container.setQueueNames("foo"); container.setChannelTransacted(true); container.setAcknowledgeMode(AcknowledgeMode.NONE); - expectedException.expect(IllegalStateException.class); - container.afterPropertiesSet(); + assertThatIllegalStateException() + .isThrownBy(() -> container.afterPropertiesSet()); container.stop(); singleConnectionFactory.destroy(); }