Skip to content

Commit 1d54f96

Browse files
committed
GH-2760: use assertThatExceptionOfType in tests
Fixes #2760 * The `assertThatExceptionOfType()` is more convenient, than `assertThatThrownBy`, so, replace all the usages accordingly * Fix JavaDoc typo in the `AbstractScriptExecutingMessageProcessor` * Fix Sonar smells for `throws Exception` in `AbstractScriptExecutingMessageProcessor` hierarchy and some code polishing for them
1 parent 82ecd5a commit 1d54f96

File tree

22 files changed

+183
-155
lines changed

22 files changed

+183
-155
lines changed

spring-integration-amqp/src/test/java/org/springframework/integration/amqp/channel/ChannelTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.integration.amqp.channel;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2121
import static org.mockito.ArgumentMatchers.any;
2222
import static org.mockito.BDDMockito.willThrow;
2323
import static org.mockito.Mockito.mock;
@@ -248,9 +248,9 @@ public void messageConversionTests() {
248248
MessageListener.class);
249249
willThrow(new MessageConversionException("foo", new IllegalStateException("bar")))
250250
.given(messageConverter).fromMessage(any(org.springframework.amqp.core.Message.class));
251-
assertThatThrownBy(() -> listener.onMessage(mock(org.springframework.amqp.core.Message.class)))
252-
.isInstanceOf(MessageConversionException.class)
253-
.hasCauseInstanceOf(IllegalStateException.class);
251+
assertThatExceptionOfType(MessageConversionException.class)
252+
.isThrownBy(() -> listener.onMessage(mock(org.springframework.amqp.core.Message.class)))
253+
.withCauseInstanceOf(IllegalStateException.class);
254254
}
255255

256256
public static class Foo {

spring-integration-core/src/test/java/org/springframework/integration/config/InvalidPriorityChannelParserTests.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.integration.config;
1818

19-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
19+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2020

2121
import org.junit.Test;
2222

@@ -34,21 +34,20 @@ public class InvalidPriorityChannelParserTests {
3434

3535
@Test
3636
public void testMessageStoreAndCapacityIllegal() {
37-
assertThatThrownBy(() ->
38-
new ClassPathXmlApplicationContext("InvalidPriorityChannelWithMessageStoreAndCapacityParserTests.xml",
39-
getClass()))
40-
.isInstanceOf(BeanDefinitionParsingException.class)
41-
.hasMessageContaining("'capacity' attribute is not allowed");
37+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
38+
.isThrownBy(() ->
39+
new ClassPathXmlApplicationContext(
40+
"InvalidPriorityChannelWithMessageStoreAndCapacityParserTests.xml", getClass()))
41+
.withMessageContaining("'capacity' attribute is not allowed");
4242
}
4343

4444
@Test
4545
public void testComparatorAndMessageStoreIllegal() {
46-
assertThatThrownBy(() ->
47-
new ClassPathXmlApplicationContext(
48-
"InvalidPriorityChannelWithComparatorAndMessageStoreParserTests.xml",
49-
getClass()))
50-
.isInstanceOf(BeanDefinitionParsingException.class)
51-
.hasMessageContaining("The 'message-store' attribute is not allowed");
46+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
47+
.isThrownBy(() ->
48+
new ClassPathXmlApplicationContext(
49+
"InvalidPriorityChannelWithComparatorAndMessageStoreParserTests.xml", getClass()))
50+
.withMessageContaining("The 'message-store' attribute is not allowed");
5251
}
5352

5453
}

spring-integration-core/src/test/java/org/springframework/integration/config/InvalidQueueChannelParserTests.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.integration.config;
1818

19-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
19+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2020

2121
import org.junit.Test;
2222

@@ -32,29 +32,29 @@ public class InvalidQueueChannelParserTests {
3232

3333
@Test
3434
public void testMessageStoreAndCapacityIllegal() {
35-
assertThatThrownBy(() ->
36-
new ClassPathXmlApplicationContext("InvalidQueueChannelWithMessageStoreAndCapacityParserTests.xml",
37-
getClass()))
38-
.isInstanceOf(BeanDefinitionParsingException.class)
39-
.hasMessageContaining("'capacity' attribute is not allowed");
35+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
36+
.isThrownBy(() ->
37+
new ClassPathXmlApplicationContext(
38+
"InvalidQueueChannelWithMessageStoreAndCapacityParserTests.xml", getClass()))
39+
.withMessageContaining("'capacity' attribute is not allowed");
4040
}
4141

4242
@Test
4343
public void testRefAndCapacityIllegal() {
44-
assertThatThrownBy(() ->
45-
new ClassPathXmlApplicationContext("InvalidQueueChannelWithRefAndCapacityParserTests.xml",
46-
getClass()))
47-
.isInstanceOf(BeanDefinitionParsingException.class)
48-
.hasMessageContaining("'capacity' attribute is not allowed");
44+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
45+
.isThrownBy(() ->
46+
new ClassPathXmlApplicationContext(
47+
"InvalidQueueChannelWithRefAndCapacityParserTests.xml", getClass()))
48+
.withMessageContaining("'capacity' attribute is not allowed");
4949
}
5050

5151
@Test
5252
public void testRefAndMessageStoreIllegal() {
53-
assertThatThrownBy(() ->
54-
new ClassPathXmlApplicationContext("InvalidQueueChannelWithRefAndMessageStoreParserTests.xml",
55-
getClass()))
56-
.isInstanceOf(BeanDefinitionParsingException.class)
57-
.hasMessageContaining("The 'message-store' attribute is not allowed " +
53+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
54+
.isThrownBy(() ->
55+
new ClassPathXmlApplicationContext(
56+
"InvalidQueueChannelWithRefAndMessageStoreParserTests.xml", getClass()))
57+
.withMessageContaining("The 'message-store' attribute is not allowed " +
5858
"when providing a 'ref' to a custom queue.");
5959
}
6060

spring-integration-core/src/test/java/org/springframework/integration/dsl/LambdaMessageProcessorTests.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.integration.dsl;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2121
import static org.assertj.core.api.Assertions.fail;
2222
import static org.mockito.BDDMockito.given;
2323
import static org.mockito.Mockito.mock;
@@ -31,6 +31,7 @@
3131
import org.springframework.integration.support.converter.ConfigurableCompositeMessageConverter;
3232
import org.springframework.integration.transformer.GenericTransformer;
3333
import org.springframework.messaging.Message;
34+
import org.springframework.messaging.MessageHandlingException;
3435
import org.springframework.messaging.converter.MessageConverter;
3536
import org.springframework.messaging.support.GenericMessage;
3637

@@ -57,7 +58,8 @@ public void testException() {
5758

5859
@Test
5960
public void testMessageAsArgument() {
60-
LambdaMessageProcessor lmp = new LambdaMessageProcessor(new GenericTransformer<Message<?>, Message<?>>() {
61+
LambdaMessageProcessor lmp = new LambdaMessageProcessor(
62+
new GenericTransformer<Message<?>, Message<?>>() { // Must not be lambda
6163

6264
@Override
6365
public Message<?> transform(Message<?> source) {
@@ -74,10 +76,12 @@ public Message<?> transform(Message<?> source) {
7476
@Test
7577
public void testMessageAsArgumentLambda() {
7678
LambdaMessageProcessor lmp = new LambdaMessageProcessor(
77-
(GenericTransformer<Message<?>, Message<?>>) source -> messageTransformer(source), null);
79+
(GenericTransformer<Message<?>, Message<?>>) this::messageTransformer, null);
7880
lmp.setBeanFactory(mock(BeanFactory.class));
7981
GenericMessage<String> testMessage = new GenericMessage<>("foo");
80-
assertThatThrownBy(() -> lmp.processMessage(testMessage)).hasCauseExactlyInstanceOf(ClassCastException.class);
82+
assertThatExceptionOfType(MessageHandlingException.class)
83+
.isThrownBy(() -> lmp.processMessage(testMessage))
84+
.withCauseInstanceOf(ClassCastException.class);
8185
}
8286

8387
private void handle(GenericHandler<?> h) {

spring-integration-core/src/test/java/org/springframework/integration/dsl/gateway/GatewayDslTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.integration.dsl.gateway;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2121

2222
import org.junit.jupiter.api.Test;
2323

@@ -87,9 +87,9 @@ void testGatewayFlow() {
8787

8888
@Test
8989
void testNestedGatewayErrorPropagation() {
90-
assertThatThrownBy(() -> this.nestedGatewayErrorPropagationFlowInput.send(new GenericMessage<>("test")))
91-
.hasCauseInstanceOf(RuntimeException.class)
92-
.hasMessageContaining("intentional");
90+
assertThatExceptionOfType(RuntimeException.class)
91+
.isThrownBy(() -> this.nestedGatewayErrorPropagationFlowInput.send(new GenericMessage<>("test")))
92+
.withMessageContaining("intentional");
9393
}
9494

9595
@Configuration

spring-integration-core/src/test/java/org/springframework/integration/dsl/manualflow/ManualFlowTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.integration.dsl.manualflow;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2121
import static org.assertj.core.api.Assertions.fail;
2222

2323
import java.util.Arrays;
@@ -513,13 +513,13 @@ public void testConcurrentRegistration() throws InterruptedException {
513513

514514
@Test
515515
public void testDisabledBeansOverride() {
516-
assertThatThrownBy(
517-
() -> this.integrationFlowContext
518-
.registration(f -> f.channel(c -> c.direct("doNotOverrideChannel")))
519-
.register())
516+
assertThatExceptionOfType(BeanCreationException.class)
517+
.isThrownBy(() ->
518+
this.integrationFlowContext.registration(f -> f.channel(c -> c.direct("doNotOverrideChannel")))
519+
.register())
520520
.isExactlyInstanceOf(BeanCreationException.class)
521-
.hasCauseExactlyInstanceOf(BeanDefinitionOverrideException.class)
522-
.hasMessageContaining("Invalid bean definition with name 'doNotOverrideChannel'");
521+
.withCauseExactlyInstanceOf(BeanDefinitionOverrideException.class)
522+
.withMessageContaining("Invalid bean definition with name 'doNotOverrideChannel'");
523523
}
524524

525525
@Configuration

spring-integration-core/src/test/java/org/springframework/integration/dsl/routers/RouterTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.integration.dsl.routers;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2121
import static org.assertj.core.api.Assertions.fail;
2222

2323
import java.util.Arrays;
@@ -587,8 +587,9 @@ public void testScatterGatherWithExecutorChannelSubFlow() {
587587

588588
@Test
589589
public void propagateErrorFromGatherer() {
590-
assertThatThrownBy(() -> propagateErrorFromGathererGateway.apply("bar"))
591-
.hasMessage("intentional");
590+
assertThatExceptionOfType(RuntimeException.class)
591+
.isThrownBy(() -> propagateErrorFromGathererGateway.apply("bar"))
592+
.withMessage("intentional");
592593
}
593594

594595
@Configuration

spring-integration-core/src/test/java/org/springframework/integration/handler/BridgeHandlerTests.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
package org.springframework.integration.handler;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2121

2222
import org.junit.Test;
2323

2424
import org.springframework.integration.channel.QueueChannel;
2525
import org.springframework.integration.support.MessageBuilder;
2626
import org.springframework.integration.test.predicate.MessagePredicate;
2727
import org.springframework.messaging.Message;
28+
import org.springframework.messaging.MessageHandlingException;
2829
import org.springframework.messaging.PollableChannel;
2930
import org.springframework.messaging.core.DestinationResolutionException;
3031
import org.springframework.messaging.support.GenericMessage;
@@ -33,6 +34,7 @@
3334
* @author Mark Fisher
3435
* @author Iwein Fuld
3536
* @author Gary Russell
37+
* @author Artem Bilan
3638
*/
3739
public class BridgeHandlerTests {
3840

@@ -41,9 +43,9 @@ public class BridgeHandlerTests {
4143
@Test
4244
public void simpleBridge() {
4345
QueueChannel outputChannel = new QueueChannel();
44-
handler.setOutputChannel(outputChannel);
46+
this.handler.setOutputChannel(outputChannel);
4547
Message<?> request = new GenericMessage<>("test");
46-
handler.handleMessage(request);
48+
this.handler.handleMessage(request);
4749
Message<?> reply = outputChannel.receive(0);
4850
assertThat(reply).isNotNull();
4951
assertThat(reply).matches(new MessagePredicate(request));
@@ -53,15 +55,16 @@ public void simpleBridge() {
5355
public void missingOutputChannelVerifiedAtRuntime() {
5456
Message<?> request = new GenericMessage<>("test");
5557

56-
assertThatThrownBy(() -> handler.handleMessage(request))
57-
.hasCauseInstanceOf(DestinationResolutionException.class);
58+
assertThatExceptionOfType(MessageHandlingException.class)
59+
.isThrownBy(() -> this.handler.handleMessage(request))
60+
.withCauseInstanceOf(DestinationResolutionException.class);
5861
}
5962

6063
@Test(timeout = 1000)
6164
public void missingOutputChannelAllowedForReplyChannelMessages() {
6265
PollableChannel replyChannel = new QueueChannel();
6366
Message<String> request = MessageBuilder.withPayload("tst").setReplyChannel(replyChannel).build();
64-
handler.handleMessage(request);
67+
this.handler.handleMessage(request);
6568
assertThat(replyChannel.receive()).matches(new MessagePredicate(request));
6669
}
6770

spring-integration-core/src/test/java/org/springframework/integration/handler/ExpressionEvaluatingMessageProcessorTests.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.integration.handler;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2121
import static org.mockito.Mockito.mock;
2222

2323
import java.util.Arrays;
@@ -42,6 +42,7 @@
4242
import org.springframework.integration.support.MessageBuilder;
4343
import org.springframework.integration.test.util.TestUtils;
4444
import org.springframework.messaging.Message;
45+
import org.springframework.messaging.MessagingException;
4546
import org.springframework.messaging.support.GenericMessage;
4647

4748
/**
@@ -211,8 +212,9 @@ public void testProcessMessageBadExpression() {
211212
ExpressionEvaluatingMessageProcessor<String> processor =
212213
new ExpressionEvaluatingMessageProcessor<>(expression);
213214
processor.setBeanFactory(mock(BeanFactory.class));
214-
assertThatThrownBy(() -> processor.processMessage(new GenericMessage<>("foo")))
215-
.hasCauseInstanceOf(EvaluationException.class);
215+
assertThatExceptionOfType(MessagingException.class)
216+
.isThrownBy(() -> processor.processMessage(new GenericMessage<>("foo")))
217+
.withCauseInstanceOf(EvaluationException.class);
216218
}
217219

218220
@Test
@@ -221,8 +223,9 @@ public void testProcessMessageExpressionThrowsRuntimeException() {
221223
ExpressionEvaluatingMessageProcessor<String> processor =
222224
new ExpressionEvaluatingMessageProcessor<>(expression);
223225
processor.setBeanFactory(mock(BeanFactory.class));
224-
assertThatThrownBy(() -> processor.processMessage(new GenericMessage<>(new TestPayload())))
225-
.hasCauseInstanceOf(UnsupportedOperationException.class);
226+
assertThatExceptionOfType(MessagingException.class)
227+
.isThrownBy(() -> processor.processMessage(new GenericMessage<>(new TestPayload())))
228+
.withCauseInstanceOf(UnsupportedOperationException.class);
226229
}
227230

228231
@Test
@@ -231,8 +234,9 @@ public void testProcessMessageExpressionThrowsCheckedException() {
231234
ExpressionEvaluatingMessageProcessor<String> processor =
232235
new ExpressionEvaluatingMessageProcessor<>(expression);
233236
processor.setBeanFactory(mock(BeanFactory.class));
234-
assertThatThrownBy(() -> processor.processMessage(new GenericMessage<>(new TestPayload())))
235-
.hasCauseInstanceOf(CheckedException.class);
237+
assertThatExceptionOfType(MessagingException.class)
238+
.isThrownBy(() -> processor.processMessage(new GenericMessage<>(new TestPayload())))
239+
.withCauseInstanceOf(CheckedException.class);
236240
}
237241

238242

spring-integration-file/src/test/java/org/springframework/integration/file/remote/StreamingInboundTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.integration.file.remote;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2121
import static org.mockito.BDDMockito.given;
2222
import static org.mockito.BDDMockito.willReturn;
2323
import static org.mockito.BDDMockito.willThrow;
@@ -132,14 +132,14 @@ public void testAllDataMaxFetch() throws Exception {
132132
}
133133

134134
@Test
135-
public void testExceptionOnFetch() throws Exception {
135+
public void testExceptionOnFetch() {
136136
StringSessionFactory sessionFactory = new StringSessionFactory();
137137
Streamer streamer = new Streamer(new StringRemoteFileTemplate(sessionFactory), null);
138138
streamer.setBeanFactory(mock(BeanFactory.class));
139139
streamer.setRemoteDirectory("/bad");
140140
streamer.afterPropertiesSet();
141-
assertThatThrownBy(streamer::receive)
142-
.isInstanceOf(MessagingException.class);
141+
assertThatExceptionOfType(MessagingException.class)
142+
.isThrownBy(streamer::receive);
143143
}
144144

145145
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)