Skip to content

Commit 15a6321

Browse files
Use assertThatException for exception test
Signed-off-by: Tran Ngoc Nhan <[email protected]>
1 parent 163049b commit 15a6321

14 files changed

+126
-213
lines changed

spring-amqp/src/test/java/org/springframework/amqp/support/converter/SimpleMessageConverterTests.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@
2727
import org.springframework.amqp.core.MessageProperties;
2828

2929
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3031
import static org.assertj.core.api.Assertions.assertThatThrownBy;
31-
import static org.assertj.core.api.Assertions.fail;
3232

3333
/**
3434
* @author Mark Fisher
3535
* @author Gary Russell
36+
* @author Ngoc Nhan
3637
*/
3738
public class SimpleMessageConverterTests extends AllowedListDeserializingMessageConverterTests {
3839

@@ -158,13 +159,9 @@ public void notConvertible() {
158159
class Foo {
159160

160161
}
161-
try {
162-
new SimpleMessageConverter().toMessage(new Foo(), new MessageProperties());
163-
fail("Expected exception");
164-
}
165-
catch (IllegalArgumentException e) {
166-
assertThat(e.getMessage()).contains("SimpleMessageConverter only supports String, byte[] and Serializable payloads, received:");
167-
}
162+
assertThatIllegalArgumentException()
163+
.isThrownBy(() -> new SimpleMessageConverter().toMessage(new Foo(), new MessageProperties()))
164+
.withMessageContaining("SimpleMessageConverter only supports String, byte[] and Serializable payloads, received:");
168165
}
169166

170167
}

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/annotation/EnableRabbitIntegrationTests.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
import org.springframework.validation.annotation.Validated;
144144

145145
import static org.assertj.core.api.Assertions.assertThat;
146-
import static org.assertj.core.api.Assertions.fail;
146+
import static org.assertj.core.api.Assertions.assertThatException;
147147
import static org.awaitility.Awaitility.await;
148148
import static org.mockito.ArgumentMatchers.any;
149149
import static org.mockito.BDDMockito.willAnswer;
@@ -158,6 +158,7 @@
158158
* @author Artem Bilan
159159
* @author Gary Russell
160160
* @author Mohammad Hewedy
161+
* @author Ngoc Nhan
161162
*
162163
* @since 1.4
163164
*/
@@ -861,13 +862,10 @@ public void deadLetterOnDefaultExchange() {
861862
@DirtiesContext
862863
public void returnExceptionWithRethrowAdapter() {
863864
this.rabbitTemplate.setMessageConverter(new RemoteInvocationAwareMessageConverterAdapter());
864-
try {
865-
this.rabbitTemplate.convertSendAndReceive("test.return.exceptions", "foo");
866-
fail("ExpectedException");
867-
}
868-
catch (Exception e) {
869-
assertThat(e.getCause().getMessage()).isEqualTo("return this");
870-
}
865+
assertThatException()
866+
.isThrownBy(() -> this.rabbitTemplate.convertSendAndReceive("test.return.exceptions", "foo"))
867+
.havingCause()
868+
.withMessage("return this");
871869
}
872870

873871
@Test
@@ -879,16 +877,15 @@ public void listenerErrorHandler() {
879877
@DirtiesContext
880878
public void listenerErrorHandlerException() {
881879
this.rabbitTemplate.setMessageConverter(new RemoteInvocationAwareMessageConverterAdapter());
882-
try {
883-
this.rabbitTemplate.convertSendAndReceive("test.pojo.errors2", "foo");
884-
fail("ExpectedException");
885-
}
886-
catch (Exception e) {
887-
assertThat(e.getCause().getMessage()).isEqualTo("from error handler");
888-
assertThat(e.getCause().getCause().getMessage()).isEqualTo("return this");
889-
EnableRabbitConfig config = this.context.getBean(EnableRabbitConfig.class);
890-
assertThat(config.errorHandlerChannel).isNotNull();
891-
}
880+
assertThatException()
881+
.isThrownBy(() -> this.rabbitTemplate.convertSendAndReceive("test.pojo.errors2", "foo"))
882+
.satisfies(ex -> {
883+
884+
assertThat(ex.getCause().getMessage()).isEqualTo("from error handler");
885+
assertThat(ex.getCause().getCause().getMessage()).isEqualTo("return this");
886+
});
887+
EnableRabbitConfig config = this.context.getBean(EnableRabbitConfig.class);
888+
assertThat(config.errorHandlerChannel).isNotNull();
892889
}
893890

894891
@Test

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/config/ListenerContainerParserTests.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@
4444
import org.springframework.test.util.ReflectionTestUtils;
4545

4646
import static org.assertj.core.api.Assertions.assertThat;
47-
import static org.assertj.core.api.Assertions.fail;
47+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4848

4949
/**
5050
* @author Mark Fisher
5151
* @author Gary Russell
5252
* @author Artem Bilan
53+
* @author Ngoc Nhan
5354
*/
5455
public class ListenerContainerParserTests {
5556

@@ -224,14 +225,11 @@ public void testAnonParent() {
224225

225226
@Test
226227
public void testIncompatibleTxAtts() {
227-
try {
228-
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-fail-context.xml", getClass()).close();
229-
fail("Parse exception expected");
230-
}
231-
catch (BeanDefinitionParsingException e) {
232-
assertThat(e.getMessage().startsWith(
233-
"Configuration problem: Listener Container - cannot set channel-transacted with acknowledge='NONE'")).isTrue();
234-
}
228+
229+
String path = getClass().getSimpleName() + "-fail-context.xml";
230+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
231+
.isThrownBy(() -> new ClassPathXmlApplicationContext(path, getClass()).close())
232+
.withMessageStartingWith("Configuration problem: Listener Container - cannot set channel-transacted with acknowledge='NONE'");
235233
}
236234

237235
@Test

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/config/MismatchedQueueDeclarationTests.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@
3131
import org.springframework.core.env.StandardEnvironment;
3232

3333
import static org.assertj.core.api.Assertions.assertThat;
34-
import static org.assertj.core.api.Assertions.fail;
34+
import static org.assertj.core.api.Assertions.assertThatException;
3535

3636
/**
3737
* @author Gary Russell
3838
* @author Gunnar Hillert
39+
* @author Ngoc Nhan
3940
* @since 1.2
4041
*
4142
*/
@@ -85,14 +86,11 @@ public void testAdminFailsWithMismatchedQueue() throws Exception {
8586
env.addActiveProfile("ttl");
8687
context.setEnvironment(env);
8788
context.refresh();
88-
channel = this.connectionFactory.createConnection().createChannel(false);
89-
try {
90-
context.getBean(CachingConnectionFactory.class).createConnection();
91-
fail("Expected exception - basic admin fails with mismatched declarations");
92-
}
93-
catch (Exception e) {
94-
assertThat(e.getCause().getCause().getMessage().contains("inequivalent arg 'x-message-ttl'")).isTrue();
95-
}
89+
this.connectionFactory.createConnection().createChannel(false);
90+
assertThatException().isThrownBy(context.getBean(CachingConnectionFactory.class)::createConnection)
91+
.satisfies(ex -> {
92+
assertThat(ex.getCause().getCause().getMessage()).contains("inequivalent arg 'x-message-ttl'");
93+
});
9694
assertThat(this.admin.getQueueProperties("mismatch.foo")).isNotNull();
9795
assertThat(this.admin.getQueueProperties("mismatch.bar")).isNull();
9896
context.close();

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/ConnectionFactoryLifecycleTests.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@
4040
import org.springframework.context.annotation.Configuration;
4141

4242
import static org.assertj.core.api.Assertions.assertThat;
43-
import static org.assertj.core.api.Assertions.fail;
43+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4444

4545
/**
4646
* @author Gary Russell
4747
* @author Artem Bilan
48+
* @author Ngoc Nhan
4849
*
4950
* @since 1.5.3
5051
*
@@ -60,13 +61,9 @@ public void testConnectionFactoryAvailableDuringStop() {
6061
context.close();
6162
assertThat(myLifecycle.isRunning()).isFalse();
6263
assertThat(TestUtils.getPropertyValue(cf, "stopped", Boolean.class)).isTrue();
63-
try {
64-
cf.createConnection();
65-
fail("Expected exception");
66-
}
67-
catch (AmqpApplicationContextClosedException e) {
68-
assertThat(e.getMessage()).contains("The ApplicationContext is closed");
69-
}
64+
assertThatExceptionOfType(AmqpApplicationContextClosedException.class)
65+
.isThrownBy(cf::createConnection)
66+
.withMessageContaining("The ApplicationContext is closed");
7067
}
7168

7269
@Test

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/SSLConnectionTests.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.springframework.core.io.ClassPathResource;
3838

3939
import static org.assertj.core.api.Assertions.assertThat;
40-
import static org.assertj.core.api.Assertions.fail;
40+
import static org.assertj.core.api.Assertions.assertThatException;
4141
import static org.mockito.BDDMockito.given;
4242
import static org.mockito.Mockito.mock;
4343
import static org.mockito.Mockito.never;
@@ -50,6 +50,7 @@
5050
* @author Gary Russell
5151
* @author Heath Abelson
5252
* @author Hareendran
53+
* @author Ngoc Nhan
5354
*
5455
* @since 1.4.4
5556
*
@@ -221,16 +222,9 @@ public void testTypeProps() {
221222
RabbitConnectionFactoryBean fb = new RabbitConnectionFactoryBean();
222223
fb.setSslPropertiesLocation(new ClassPathResource("ssl.properties"));
223224
fb.afterPropertiesSet();
224-
try {
225-
fb.setUpSSL();
226-
//Here we make sure the exception is thrown because setUpSSL() will fail.
227-
// But we only care about having it load the props
228-
fail("setupSSL should fail");
229-
}
230-
catch (Exception e) {
231-
assertThat(fb.getKeyStoreType()).isEqualTo("foo");
232-
assertThat(fb.getTrustStoreType()).isEqualTo("bar");
233-
}
225+
assertThatException().isThrownBy(fb::setUpSSL);
226+
assertThat(fb.getKeyStoreType()).isEqualTo("foo");
227+
assertThat(fb.getTrustStoreType()).isEqualTo("bar");
234228
}
235229

236230
@Test
@@ -249,16 +243,9 @@ public void testTypeSettersOverrideProps() {
249243
fb.afterPropertiesSet();
250244
fb.setKeyStoreType("alice");
251245
fb.setTrustStoreType("bob");
252-
try {
253-
fb.setUpSSL();
254-
// Here we make sure the exception is thrown because setUpSSL() will fail.
255-
//But we only care about having it load the props
256-
fail("setupSSL should fail");
257-
}
258-
catch (Exception e) {
259-
assertThat(fb.getKeyStoreType()).isEqualTo("alice");
260-
assertThat(fb.getTrustStoreType()).isEqualTo("bob");
261-
}
246+
assertThatException().isThrownBy(fb::setUpSSL);
247+
assertThat(fb.getKeyStoreType()).isEqualTo("alice");
248+
assertThat(fb.getTrustStoreType()).isEqualTo("bob");
262249
}
263250

264251
@Test

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitAdminTests.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@
6767
import org.springframework.retry.support.RetryTemplate;
6868

6969
import static org.assertj.core.api.Assertions.assertThat;
70+
import static org.assertj.core.api.Assertions.assertThatException;
7071
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
7172
import static org.assertj.core.api.Assertions.assertThatThrownBy;
72-
import static org.assertj.core.api.Assertions.fail;
7373
import static org.awaitility.Awaitility.await;
7474
import static org.mockito.ArgumentMatchers.any;
7575
import static org.mockito.ArgumentMatchers.anyBoolean;
@@ -92,6 +92,7 @@
9292
* @author Gary Russell
9393
* @author Artem Bilan
9494
* @author Artem Yakshin
95+
* @author Ngoc Nhan
9596
*
9697
* @since 1.4.1
9798
*
@@ -102,13 +103,8 @@ public class RabbitAdminTests extends NeedsManagementTests {
102103
@Test
103104
public void testSettingOfNullConnectionFactory() {
104105
ConnectionFactory connectionFactory = null;
105-
try {
106-
new RabbitAdmin(connectionFactory);
107-
fail("should have thrown IllegalArgumentException when ConnectionFactory is null.");
108-
}
109-
catch (IllegalArgumentException e) {
110-
assertThat(e.getMessage()).isEqualTo("ConnectionFactory must not be null");
111-
}
106+
assertThatIllegalArgumentException().isThrownBy(() -> new RabbitAdmin(connectionFactory))
107+
.withMessage("ConnectionFactory must not be null");
112108
}
113109

114110
@Test
@@ -266,13 +262,7 @@ public void testAvoidHangAMQP_508() {
266262
CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
267263
RabbitAdmin admin = new RabbitAdmin(cf);
268264
String longName = new String(new byte[300]).replace('\u0000', 'x');
269-
try {
270-
admin.declareQueue(new Queue(longName));
271-
fail("expected exception");
272-
}
273-
catch (@SuppressWarnings("unused") Exception e) {
274-
// NOSONAR
275-
}
265+
assertThatException().isThrownBy(() -> admin.declareQueue(new Queue(longName)));
276266
String goodName = "foobar";
277267
admin.declareQueue(new Queue(goodName));
278268
assertThat(admin.getQueueProperties(longName)).isNull();

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateTests.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
8686
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
8787
import static org.assertj.core.api.Assertions.assertThatThrownBy;
88-
import static org.assertj.core.api.Assertions.fail;
8988
import static org.mockito.ArgumentMatchers.any;
9089
import static org.mockito.ArgumentMatchers.anyBoolean;
9190
import static org.mockito.ArgumentMatchers.anyMap;
@@ -104,6 +103,7 @@
104103
* @author Gary Russell
105104
* @author Artem Bilan
106105
* @author Mohammad Hewedy
106+
* @author Ngoc Nhan
107107
* @since 1.0.1
108108
*
109109
*/
@@ -492,13 +492,10 @@ public void testShutdownWhileWaitingForReply() throws Exception {
492492
}
493493
listener.get().shutdownCompleted(new ShutdownSignalException(true, false, null, null));
494494
});
495-
try {
496-
template.doSendAndReceiveWithTemporary("foo", "bar", input, null);
497-
fail("Expected exception");
498-
}
499-
catch (AmqpException e) {
500-
assertThat(e.getCause()).isInstanceOf(ShutdownSignalException.class);
501-
}
495+
496+
assertThatExceptionOfType(AmqpException.class)
497+
.isThrownBy(() -> template.doSendAndReceiveWithTemporary("foo", "bar", input, null))
498+
.withCauseInstanceOf(ShutdownSignalException.class);
502499
exec.shutdownNow();
503500
}
504501

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/BlockingQueueConsumerIntegrationTests.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@
3636
import org.springframework.amqp.rabbit.support.DefaultMessagePropertiesConverter;
3737

3838
import static org.assertj.core.api.Assertions.assertThat;
39-
import static org.assertj.core.api.Assertions.fail;
39+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4040

4141
/**
4242
* @author Dave Syer
4343
* @author Gunnar Hillert
4444
* @author Gary Russell
45+
* @author Ngoc Nhan
4546
* @since 1.0
4647
*
4748
*/
@@ -98,13 +99,9 @@ public void testAvoidHangAMQP_508() {
9899
BlockingQueueConsumer blockingQueueConsumer = new BlockingQueueConsumer(connectionFactory,
99100
new DefaultMessagePropertiesConverter(), new ActiveObjectCounter<BlockingQueueConsumer>(),
100101
AcknowledgeMode.AUTO, true, 1, longName, "foobar");
101-
try {
102-
blockingQueueConsumer.start();
103-
fail("expected exception");
104-
}
105-
catch (FatalListenerStartupException e) {
106-
assertThat(e.getCause()).isInstanceOf(IllegalArgumentException.class);
107-
}
102+
assertThatExceptionOfType(FatalListenerStartupException.class)
103+
.isThrownBy(blockingQueueConsumer::start)
104+
.withCauseInstanceOf(IllegalArgumentException.class);
108105
connectionFactory.destroy();
109106
}
110107

0 commit comments

Comments
 (0)