GH-2760: use assertThatExceptionOfType in tests

Fixes https://github.com/spring-projects/spring-integration/issues/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
This commit is contained in:
Artem Bilan
2019-02-22 13:54:36 -05:00
parent 82ecd5a75d
commit 1d54f9663a
22 changed files with 187 additions and 159 deletions

View File

@@ -16,7 +16,7 @@
package org.springframework.integration.config;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import org.junit.Test;
@@ -34,21 +34,20 @@ public class InvalidPriorityChannelParserTests {
@Test
public void testMessageStoreAndCapacityIllegal() {
assertThatThrownBy(() ->
new ClassPathXmlApplicationContext("InvalidPriorityChannelWithMessageStoreAndCapacityParserTests.xml",
getClass()))
.isInstanceOf(BeanDefinitionParsingException.class)
.hasMessageContaining("'capacity' attribute is not allowed");
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() ->
new ClassPathXmlApplicationContext(
"InvalidPriorityChannelWithMessageStoreAndCapacityParserTests.xml", getClass()))
.withMessageContaining("'capacity' attribute is not allowed");
}
@Test
public void testComparatorAndMessageStoreIllegal() {
assertThatThrownBy(() ->
new ClassPathXmlApplicationContext(
"InvalidPriorityChannelWithComparatorAndMessageStoreParserTests.xml",
getClass()))
.isInstanceOf(BeanDefinitionParsingException.class)
.hasMessageContaining("The 'message-store' attribute is not allowed");
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() ->
new ClassPathXmlApplicationContext(
"InvalidPriorityChannelWithComparatorAndMessageStoreParserTests.xml", getClass()))
.withMessageContaining("The 'message-store' attribute is not allowed");
}
}

View File

@@ -16,7 +16,7 @@
package org.springframework.integration.config;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import org.junit.Test;
@@ -32,29 +32,29 @@ public class InvalidQueueChannelParserTests {
@Test
public void testMessageStoreAndCapacityIllegal() {
assertThatThrownBy(() ->
new ClassPathXmlApplicationContext("InvalidQueueChannelWithMessageStoreAndCapacityParserTests.xml",
getClass()))
.isInstanceOf(BeanDefinitionParsingException.class)
.hasMessageContaining("'capacity' attribute is not allowed");
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() ->
new ClassPathXmlApplicationContext(
"InvalidQueueChannelWithMessageStoreAndCapacityParserTests.xml", getClass()))
.withMessageContaining("'capacity' attribute is not allowed");
}
@Test
public void testRefAndCapacityIllegal() {
assertThatThrownBy(() ->
new ClassPathXmlApplicationContext("InvalidQueueChannelWithRefAndCapacityParserTests.xml",
getClass()))
.isInstanceOf(BeanDefinitionParsingException.class)
.hasMessageContaining("'capacity' attribute is not allowed");
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() ->
new ClassPathXmlApplicationContext(
"InvalidQueueChannelWithRefAndCapacityParserTests.xml", getClass()))
.withMessageContaining("'capacity' attribute is not allowed");
}
@Test
public void testRefAndMessageStoreIllegal() {
assertThatThrownBy(() ->
new ClassPathXmlApplicationContext("InvalidQueueChannelWithRefAndMessageStoreParserTests.xml",
getClass()))
.isInstanceOf(BeanDefinitionParsingException.class)
.hasMessageContaining("The 'message-store' attribute is not allowed " +
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() ->
new ClassPathXmlApplicationContext(
"InvalidQueueChannelWithRefAndMessageStoreParserTests.xml", getClass()))
.withMessageContaining("The 'message-store' attribute is not allowed " +
"when providing a 'ref' to a custom queue.");
}

View File

@@ -17,7 +17,7 @@
package org.springframework.integration.dsl;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -31,6 +31,7 @@ import org.springframework.integration.handler.LambdaMessageProcessor;
import org.springframework.integration.support.converter.ConfigurableCompositeMessageConverter;
import org.springframework.integration.transformer.GenericTransformer;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.support.GenericMessage;
@@ -57,7 +58,8 @@ public class LambdaMessageProcessorTests {
@Test
public void testMessageAsArgument() {
LambdaMessageProcessor lmp = new LambdaMessageProcessor(new GenericTransformer<Message<?>, Message<?>>() {
LambdaMessageProcessor lmp = new LambdaMessageProcessor(
new GenericTransformer<Message<?>, Message<?>>() { // Must not be lambda
@Override
public Message<?> transform(Message<?> source) {
@@ -74,10 +76,12 @@ public class LambdaMessageProcessorTests {
@Test
public void testMessageAsArgumentLambda() {
LambdaMessageProcessor lmp = new LambdaMessageProcessor(
(GenericTransformer<Message<?>, Message<?>>) source -> messageTransformer(source), null);
(GenericTransformer<Message<?>, Message<?>>) this::messageTransformer, null);
lmp.setBeanFactory(mock(BeanFactory.class));
GenericMessage<String> testMessage = new GenericMessage<>("foo");
assertThatThrownBy(() -> lmp.processMessage(testMessage)).hasCauseExactlyInstanceOf(ClassCastException.class);
assertThatExceptionOfType(MessageHandlingException.class)
.isThrownBy(() -> lmp.processMessage(testMessage))
.withCauseInstanceOf(ClassCastException.class);
}
private void handle(GenericHandler<?> h) {

View File

@@ -17,7 +17,7 @@
package org.springframework.integration.dsl.gateway;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import org.junit.jupiter.api.Test;
@@ -87,9 +87,9 @@ public class GatewayDslTests {
@Test
void testNestedGatewayErrorPropagation() {
assertThatThrownBy(() -> this.nestedGatewayErrorPropagationFlowInput.send(new GenericMessage<>("test")))
.hasCauseInstanceOf(RuntimeException.class)
.hasMessageContaining("intentional");
assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> this.nestedGatewayErrorPropagationFlowInput.send(new GenericMessage<>("test")))
.withMessageContaining("intentional");
}
@Configuration

View File

@@ -17,7 +17,7 @@
package org.springframework.integration.dsl.manualflow;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.fail;
import java.util.Arrays;
@@ -513,13 +513,13 @@ public class ManualFlowTests {
@Test
public void testDisabledBeansOverride() {
assertThatThrownBy(
() -> this.integrationFlowContext
.registration(f -> f.channel(c -> c.direct("doNotOverrideChannel")))
.register())
assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(() ->
this.integrationFlowContext.registration(f -> f.channel(c -> c.direct("doNotOverrideChannel")))
.register())
.isExactlyInstanceOf(BeanCreationException.class)
.hasCauseExactlyInstanceOf(BeanDefinitionOverrideException.class)
.hasMessageContaining("Invalid bean definition with name 'doNotOverrideChannel'");
.withCauseExactlyInstanceOf(BeanDefinitionOverrideException.class)
.withMessageContaining("Invalid bean definition with name 'doNotOverrideChannel'");
}
@Configuration

View File

@@ -17,7 +17,7 @@
package org.springframework.integration.dsl.routers;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.fail;
import java.util.Arrays;
@@ -587,8 +587,9 @@ public class RouterTests {
@Test
public void propagateErrorFromGatherer() {
assertThatThrownBy(() -> propagateErrorFromGathererGateway.apply("bar"))
.hasMessage("intentional");
assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> propagateErrorFromGathererGateway.apply("bar"))
.withMessage("intentional");
}
@Configuration

View File

@@ -17,7 +17,7 @@
package org.springframework.integration.handler;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import org.junit.Test;
@@ -25,6 +25,7 @@ import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.predicate.MessagePredicate;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.core.DestinationResolutionException;
import org.springframework.messaging.support.GenericMessage;
@@ -33,6 +34,7 @@ import org.springframework.messaging.support.GenericMessage;
* @author Mark Fisher
* @author Iwein Fuld
* @author Gary Russell
* @author Artem Bilan
*/
public class BridgeHandlerTests {
@@ -41,9 +43,9 @@ public class BridgeHandlerTests {
@Test
public void simpleBridge() {
QueueChannel outputChannel = new QueueChannel();
handler.setOutputChannel(outputChannel);
this.handler.setOutputChannel(outputChannel);
Message<?> request = new GenericMessage<>("test");
handler.handleMessage(request);
this.handler.handleMessage(request);
Message<?> reply = outputChannel.receive(0);
assertThat(reply).isNotNull();
assertThat(reply).matches(new MessagePredicate(request));
@@ -53,15 +55,16 @@ public class BridgeHandlerTests {
public void missingOutputChannelVerifiedAtRuntime() {
Message<?> request = new GenericMessage<>("test");
assertThatThrownBy(() -> handler.handleMessage(request))
.hasCauseInstanceOf(DestinationResolutionException.class);
assertThatExceptionOfType(MessageHandlingException.class)
.isThrownBy(() -> this.handler.handleMessage(request))
.withCauseInstanceOf(DestinationResolutionException.class);
}
@Test(timeout = 1000)
public void missingOutputChannelAllowedForReplyChannelMessages() {
PollableChannel replyChannel = new QueueChannel();
Message<String> request = MessageBuilder.withPayload("tst").setReplyChannel(replyChannel).build();
handler.handleMessage(request);
this.handler.handleMessage(request);
assertThat(replyChannel.receive()).matches(new MessagePredicate(request));
}

View File

@@ -17,7 +17,7 @@
package org.springframework.integration.handler;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.mock;
import java.util.Arrays;
@@ -42,6 +42,7 @@ import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.support.GenericMessage;
/**
@@ -211,8 +212,9 @@ public class ExpressionEvaluatingMessageProcessorTests {
ExpressionEvaluatingMessageProcessor<String> processor =
new ExpressionEvaluatingMessageProcessor<>(expression);
processor.setBeanFactory(mock(BeanFactory.class));
assertThatThrownBy(() -> processor.processMessage(new GenericMessage<>("foo")))
.hasCauseInstanceOf(EvaluationException.class);
assertThatExceptionOfType(MessagingException.class)
.isThrownBy(() -> processor.processMessage(new GenericMessage<>("foo")))
.withCauseInstanceOf(EvaluationException.class);
}
@Test
@@ -221,8 +223,9 @@ public class ExpressionEvaluatingMessageProcessorTests {
ExpressionEvaluatingMessageProcessor<String> processor =
new ExpressionEvaluatingMessageProcessor<>(expression);
processor.setBeanFactory(mock(BeanFactory.class));
assertThatThrownBy(() -> processor.processMessage(new GenericMessage<>(new TestPayload())))
.hasCauseInstanceOf(UnsupportedOperationException.class);
assertThatExceptionOfType(MessagingException.class)
.isThrownBy(() -> processor.processMessage(new GenericMessage<>(new TestPayload())))
.withCauseInstanceOf(UnsupportedOperationException.class);
}
@Test
@@ -231,8 +234,9 @@ public class ExpressionEvaluatingMessageProcessorTests {
ExpressionEvaluatingMessageProcessor<String> processor =
new ExpressionEvaluatingMessageProcessor<>(expression);
processor.setBeanFactory(mock(BeanFactory.class));
assertThatThrownBy(() -> processor.processMessage(new GenericMessage<>(new TestPayload())))
.hasCauseInstanceOf(CheckedException.class);
assertThatExceptionOfType(MessagingException.class)
.isThrownBy(() -> processor.processMessage(new GenericMessage<>(new TestPayload())))
.withCauseInstanceOf(CheckedException.class);
}