Skip to content

Commit

Permalink
Use assertThatThrownBy() Vs. ExpectedException
Browse files Browse the repository at this point in the history
Also avoid `access()` method in `RabbitTemplatePublisherCallbacksIntegrationTests3`.
  • Loading branch information
garyrussell authored and artembilan committed Apr 17, 2019
1 parent c1ec3e0 commit 4ef8e96
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

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;
Expand All @@ -38,9 +36,6 @@
*/
public abstract class AbstractRabbitAnnotationDrivenTests {

@Rule
public final ExpectedException thrown = ExpectedException.none();

@Test
public abstract void rabbitListenerIsRepeatable();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
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;
Expand Down Expand Up @@ -54,9 +52,6 @@
*/
public class RabbitListenerContainerFactoryTests {

@Rule
public final ExpectedException thrown = ExpectedException.none();

private final SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();

private final DirectRabbitListenerContainerFactory direct = new DirectRabbitListenerContainerFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@


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;
Expand All @@ -35,9 +34,6 @@
*/
public class SimpleRabbitListenerEndpointTests {

@Rule
public final ExpectedException thrown = ExpectedException.none();

private final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();

private final MessageListener messageListener = new MessageListenerAdapter();
Expand All @@ -57,8 +53,8 @@ public void queueAndQueueNamesSet() {
endpoint.setQueueNames("foo", "bar");
endpoint.setQueues(mock(Queue.class));

thrown.expect(IllegalStateException.class);
endpoint.setupListenerContainer(container);
assertThatIllegalStateException()
.isThrownBy(() -> endpoint.setupListenerContainer(container));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,7 +48,6 @@
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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -270,20 +265,21 @@ public void testSendAndReceiveFromVolatileQueue() throws Exception {
}

@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
Expand All @@ -296,13 +292,11 @@ public void testSendAndReceiveFromVolatileQueueAfterImplicitRemoval() throws Exc
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();
}

Expand All @@ -321,13 +315,12 @@ public void testMixTransactionalAndNonTransactional() throws Exception {
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);

}

Expand Down Expand Up @@ -370,12 +363,12 @@ public void testHardErrorAndReconnectNoAuto() throws Exception {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,7 +48,6 @@
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;
Expand Down Expand Up @@ -97,9 +97,6 @@
*/
public class RabbitAdminTests {

@Rule
public ExpectedException exception = ExpectedException.none();

@Rule
public BrokerRunning brokerIsRunning = BrokerRunning.isBrokerAndManagementRunning();

Expand Down Expand Up @@ -138,8 +135,7 @@ public void testFailOnFirstUseWithMissingBroker() {
rabbitAdmin.setApplicationContext(applicationContext);
rabbitAdmin.setAutoStartup(true);
rabbitAdmin.afterPropertiesSet();
exception.expect(IllegalArgumentException.class);
rabbitAdmin.declareQueue();
assertThatIllegalArgumentException().isThrownBy(() -> rabbitAdmin.declareQueue());
connectionFactory.destroy();
}

Expand Down
Loading

0 comments on commit 4ef8e96

Please sign in to comment.