Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
import org.springframework.amqp.core.MessageProperties;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;

/**
* @author Mark Fisher
* @author Gary Russell
* @author Ngoc Nhan
*/
public class SimpleMessageConverterTests extends AllowedListDeserializingMessageConverterTests {

Expand Down Expand Up @@ -158,13 +159,9 @@ public void notConvertible() {
class Foo {

}
try {
new SimpleMessageConverter().toMessage(new Foo(), new MessageProperties());
fail("Expected exception");
}
catch (IllegalArgumentException e) {
assertThat(e.getMessage()).contains("SimpleMessageConverter only supports String, byte[] and Serializable payloads, received:");
}
assertThatIllegalArgumentException()
.isThrownBy(() -> new SimpleMessageConverter().toMessage(new Foo(), new MessageProperties()))
.withMessageContaining("SimpleMessageConverter only supports String, byte[] and Serializable payloads, received:");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
import org.springframework.validation.annotation.Validated;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.awaitility.Awaitility.await;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.willAnswer;
Expand All @@ -158,6 +158,7 @@
* @author Artem Bilan
* @author Gary Russell
* @author Mohammad Hewedy
* @author Ngoc Nhan
*
* @since 1.4
*/
Expand Down Expand Up @@ -861,13 +862,10 @@ public void deadLetterOnDefaultExchange() {
@DirtiesContext
public void returnExceptionWithRethrowAdapter() {
this.rabbitTemplate.setMessageConverter(new RemoteInvocationAwareMessageConverterAdapter());
try {
this.rabbitTemplate.convertSendAndReceive("test.return.exceptions", "foo");
fail("ExpectedException");
}
catch (Exception e) {
assertThat(e.getCause().getMessage()).isEqualTo("return this");
}
assertThatException()
.isThrownBy(() -> this.rabbitTemplate.convertSendAndReceive("test.return.exceptions", "foo"))
.havingCause()
.withMessage("return this");
}

@Test
Expand All @@ -879,16 +877,15 @@ public void listenerErrorHandler() {
@DirtiesContext
public void listenerErrorHandlerException() {
this.rabbitTemplate.setMessageConverter(new RemoteInvocationAwareMessageConverterAdapter());
try {
this.rabbitTemplate.convertSendAndReceive("test.pojo.errors2", "foo");
fail("ExpectedException");
}
catch (Exception e) {
assertThat(e.getCause().getMessage()).isEqualTo("from error handler");
assertThat(e.getCause().getCause().getMessage()).isEqualTo("return this");
EnableRabbitConfig config = this.context.getBean(EnableRabbitConfig.class);
assertThat(config.errorHandlerChannel).isNotNull();
}
assertThatException()
.isThrownBy(() -> this.rabbitTemplate.convertSendAndReceive("test.pojo.errors2", "foo"))
.satisfies(ex -> {

assertThat(ex.getCause().getMessage()).isEqualTo("from error handler");
assertThat(ex.getCause().getCause().getMessage()).isEqualTo("return this");
EnableRabbitConfig config = this.context.getBean(EnableRabbitConfig.class);
assertThat(config.errorHandlerChannel).isNotNull();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these two last lines could be outside of the exception assertion scope.

});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@
import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

/**
* @author Mark Fisher
* @author Gary Russell
* @author Artem Bilan
* @author Ngoc Nhan
*/
public class ListenerContainerParserTests {

Expand Down Expand Up @@ -224,14 +225,11 @@ public void testAnonParent() {

@Test
public void testIncompatibleTxAtts() {
try {
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-fail-context.xml", getClass()).close();
fail("Parse exception expected");
}
catch (BeanDefinitionParsingException e) {
assertThat(e.getMessage().startsWith(
"Configuration problem: Listener Container - cannot set channel-transacted with acknowledge='NONE'")).isTrue();
}

String path = getClass().getSimpleName() + "-fail-context.xml";
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() -> new ClassPathXmlApplicationContext(path, getClass()).close())
.withMessageStartingWith("Configuration problem: Listener Container - cannot set channel-transacted with acknowledge='NONE'");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
import org.springframework.core.env.StandardEnvironment;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatException;

/**
* @author Gary Russell
* @author Gunnar Hillert
* @author Ngoc Nhan
* @since 1.2
*
*/
Expand Down Expand Up @@ -85,14 +86,11 @@ public void testAdminFailsWithMismatchedQueue() throws Exception {
env.addActiveProfile("ttl");
context.setEnvironment(env);
context.refresh();
channel = this.connectionFactory.createConnection().createChannel(false);
try {
context.getBean(CachingConnectionFactory.class).createConnection();
fail("Expected exception - basic admin fails with mismatched declarations");
}
catch (Exception e) {
assertThat(e.getCause().getCause().getMessage().contains("inequivalent arg 'x-message-ttl'")).isTrue();
}
this.connectionFactory.createConnection().createChannel(false);
assertThatException().isThrownBy(context.getBean(CachingConnectionFactory.class)::createConnection)
.satisfies(ex -> {
assertThat(ex.getCause().getCause().getMessage()).contains("inequivalent arg 'x-message-ttl'");
});
assertThat(this.admin.getQueueProperties("mismatch.foo")).isNotNull();
assertThat(this.admin.getQueueProperties("mismatch.bar")).isNull();
context.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@
import org.springframework.context.annotation.Configuration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

/**
* @author Gary Russell
* @author Artem Bilan
* @author Ngoc Nhan
*
* @since 1.5.3
*
Expand All @@ -60,13 +61,9 @@ public void testConnectionFactoryAvailableDuringStop() {
context.close();
assertThat(myLifecycle.isRunning()).isFalse();
assertThat(TestUtils.getPropertyValue(cf, "stopped", Boolean.class)).isTrue();
try {
cf.createConnection();
fail("Expected exception");
}
catch (AmqpApplicationContextClosedException e) {
assertThat(e.getMessage()).contains("The ApplicationContext is closed");
}
assertThatExceptionOfType(AmqpApplicationContextClosedException.class)
.isThrownBy(cf::createConnection)
.withMessageContaining("The ApplicationContext is closed");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.springframework.core.io.ClassPathResource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
Expand All @@ -50,6 +50,7 @@
* @author Gary Russell
* @author Heath Abelson
* @author Hareendran
* @author Ngoc Nhan
*
* @since 1.4.4
*
Expand Down Expand Up @@ -221,16 +222,9 @@ public void testTypeProps() {
RabbitConnectionFactoryBean fb = new RabbitConnectionFactoryBean();
fb.setSslPropertiesLocation(new ClassPathResource("ssl.properties"));
fb.afterPropertiesSet();
try {
fb.setUpSSL();
//Here we make sure the exception is thrown because setUpSSL() will fail.
// But we only care about having it load the props
fail("setupSSL should fail");
}
catch (Exception e) {
assertThat(fb.getKeyStoreType()).isEqualTo("foo");
assertThat(fb.getTrustStoreType()).isEqualTo("bar");
}
assertThatException().isThrownBy(fb::setUpSSL);
assertThat(fb.getKeyStoreType()).isEqualTo("foo");
assertThat(fb.getTrustStoreType()).isEqualTo("bar");
}

@Test
Expand All @@ -249,16 +243,9 @@ public void testTypeSettersOverrideProps() {
fb.afterPropertiesSet();
fb.setKeyStoreType("alice");
fb.setTrustStoreType("bob");
try {
fb.setUpSSL();
// Here we make sure the exception is thrown because setUpSSL() will fail.
//But we only care about having it load the props
fail("setupSSL should fail");
}
catch (Exception e) {
assertThat(fb.getKeyStoreType()).isEqualTo("alice");
assertThat(fb.getTrustStoreType()).isEqualTo("bob");
}
assertThatException().isThrownBy(fb::setUpSSL);
assertThat(fb.getKeyStoreType()).isEqualTo("alice");
assertThat(fb.getTrustStoreType()).isEqualTo("bob");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@
import org.springframework.retry.support.RetryTemplate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
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.anyBoolean;
Expand All @@ -92,6 +92,7 @@
* @author Gary Russell
* @author Artem Bilan
* @author Artem Yakshin
* @author Ngoc Nhan
*
* @since 1.4.1
*
Expand All @@ -102,13 +103,8 @@ public class RabbitAdminTests extends NeedsManagementTests {
@Test
public void testSettingOfNullConnectionFactory() {
ConnectionFactory connectionFactory = null;
try {
new RabbitAdmin(connectionFactory);
fail("should have thrown IllegalArgumentException when ConnectionFactory is null.");
}
catch (IllegalArgumentException e) {
assertThat(e.getMessage()).isEqualTo("ConnectionFactory must not be null");
}
assertThatIllegalArgumentException().isThrownBy(() -> new RabbitAdmin(connectionFactory))
.withMessage("ConnectionFactory must not be null");
}

@Test
Expand Down Expand Up @@ -266,13 +262,7 @@ public void testAvoidHangAMQP_508() {
CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
RabbitAdmin admin = new RabbitAdmin(cf);
String longName = new String(new byte[300]).replace('\u0000', 'x');
try {
admin.declareQueue(new Queue(longName));
fail("expected exception");
}
catch (@SuppressWarnings("unused") Exception e) {
// NOSONAR
}
assertThatException().isThrownBy(() -> admin.declareQueue(new Queue(longName)));
String goodName = "foobar";
admin.declareQueue(new Queue(goodName));
assertThat(admin.getQueueProperties(longName)).isNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
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.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyMap;
Expand All @@ -104,6 +103,7 @@
* @author Gary Russell
* @author Artem Bilan
* @author Mohammad Hewedy
* @author Ngoc Nhan
* @since 1.0.1
*
*/
Expand Down Expand Up @@ -492,13 +492,10 @@ public void testShutdownWhileWaitingForReply() throws Exception {
}
listener.get().shutdownCompleted(new ShutdownSignalException(true, false, null, null));
});
try {
template.doSendAndReceiveWithTemporary("foo", "bar", input, null);
fail("Expected exception");
}
catch (AmqpException e) {
assertThat(e.getCause()).isInstanceOf(ShutdownSignalException.class);
}

assertThatExceptionOfType(AmqpException.class)
.isThrownBy(() -> template.doSendAndReceiveWithTemporary("foo", "bar", input, null))
.withCauseInstanceOf(ShutdownSignalException.class);
exec.shutdownNow();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@
import org.springframework.amqp.rabbit.support.DefaultMessagePropertiesConverter;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

/**
* @author Dave Syer
* @author Gunnar Hillert
* @author Gary Russell
* @author Ngoc Nhan
* @since 1.0
*
*/
Expand Down Expand Up @@ -98,13 +99,9 @@ public void testAvoidHangAMQP_508() {
BlockingQueueConsumer blockingQueueConsumer = new BlockingQueueConsumer(connectionFactory,
new DefaultMessagePropertiesConverter(), new ActiveObjectCounter<BlockingQueueConsumer>(),
AcknowledgeMode.AUTO, true, 1, longName, "foobar");
try {
blockingQueueConsumer.start();
fail("expected exception");
}
catch (FatalListenerStartupException e) {
assertThat(e.getCause()).isInstanceOf(IllegalArgumentException.class);
}
assertThatExceptionOfType(FatalListenerStartupException.class)
.isThrownBy(blockingQueueConsumer::start)
.withCauseInstanceOf(IllegalArgumentException.class);
connectionFactory.destroy();
}

Expand Down
Loading