From 1d54f9663a8cdc648f3a9360e36c3e18ca59b6ad Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Fri, 22 Feb 2019 13:54:36 -0500 Subject: [PATCH] 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 --- .../amqp/channel/ChannelTests.java | 8 ++-- .../InvalidPriorityChannelParserTests.java | 23 +++++---- .../InvalidQueueChannelParserTests.java | 32 ++++++------- .../dsl/LambdaMessageProcessorTests.java | 12 +++-- .../dsl/gateway/GatewayDslTests.java | 8 ++-- .../dsl/manualflow/ManualFlowTests.java | 14 +++--- .../integration/dsl/routers/RouterTests.java | 7 +-- .../handler/BridgeHandlerTests.java | 15 +++--- ...essionEvaluatingMessageProcessorTests.java | 18 ++++--- .../file/remote/StreamingInboundTests.java | 8 ++-- .../groovy/GroovyCommandMessageProcessor.java | 34 ++++++++------ ...GroovyScriptExecutingMessageProcessor.java | 47 +++++++++++-------- .../jpa/core/JpaExecutorTests.java | 8 ++-- .../redis/util/RedisLockRegistryTests.java | 8 ++-- ...stractScriptExecutingMessageProcessor.java | 35 +++++++------- .../ScriptExecutingMessageProcessor.java | 12 ++--- .../ws/config/UriVariableTests.java | 8 ++-- .../xml/DefaultXmlPayloadConverterTests.java | 8 ++-- .../xml/source/DomSourceFactoryTests.java | 8 ++-- .../xml/source/StringSourceTests.java | 8 ++-- .../ResultToStringTransformerTests.java | 8 ++-- .../XsltPayloadTransformerTests.java | 17 +++---- 22 files changed, 187 insertions(+), 159 deletions(-) diff --git a/spring-integration-amqp/src/test/java/org/springframework/integration/amqp/channel/ChannelTests.java b/spring-integration-amqp/src/test/java/org/springframework/integration/amqp/channel/ChannelTests.java index ee22e3e5f3..c2806b6956 100644 --- a/spring-integration-amqp/src/test/java/org/springframework/integration/amqp/channel/ChannelTests.java +++ b/spring-integration-amqp/src/test/java/org/springframework/integration/amqp/channel/ChannelTests.java @@ -17,7 +17,7 @@ package org.springframework.integration.amqp.channel; 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.ArgumentMatchers.any; import static org.mockito.BDDMockito.willThrow; import static org.mockito.Mockito.mock; @@ -248,9 +248,9 @@ public class ChannelTests { MessageListener.class); willThrow(new MessageConversionException("foo", new IllegalStateException("bar"))) .given(messageConverter).fromMessage(any(org.springframework.amqp.core.Message.class)); - assertThatThrownBy(() -> listener.onMessage(mock(org.springframework.amqp.core.Message.class))) - .isInstanceOf(MessageConversionException.class) - .hasCauseInstanceOf(IllegalStateException.class); + assertThatExceptionOfType(MessageConversionException.class) + .isThrownBy(() -> listener.onMessage(mock(org.springframework.amqp.core.Message.class))) + .withCauseInstanceOf(IllegalStateException.class); } public static class Foo { diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/InvalidPriorityChannelParserTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/InvalidPriorityChannelParserTests.java index 2e1e69890f..3ff452e8ee 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/InvalidPriorityChannelParserTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/InvalidPriorityChannelParserTests.java @@ -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"); } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/InvalidQueueChannelParserTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/InvalidQueueChannelParserTests.java index 50594ca9af..0859ee49a9 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/InvalidQueueChannelParserTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/InvalidQueueChannelParserTests.java @@ -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."); } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/dsl/LambdaMessageProcessorTests.java b/spring-integration-core/src/test/java/org/springframework/integration/dsl/LambdaMessageProcessorTests.java index 3e8dc9f547..49ff45f926 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/dsl/LambdaMessageProcessorTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/dsl/LambdaMessageProcessorTests.java @@ -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>() { + LambdaMessageProcessor lmp = new LambdaMessageProcessor( + new GenericTransformer, 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>) source -> messageTransformer(source), null); + (GenericTransformer, Message>) this::messageTransformer, null); lmp.setBeanFactory(mock(BeanFactory.class)); GenericMessage 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) { diff --git a/spring-integration-core/src/test/java/org/springframework/integration/dsl/gateway/GatewayDslTests.java b/spring-integration-core/src/test/java/org/springframework/integration/dsl/gateway/GatewayDslTests.java index 495d3c5c25..f387fc1819 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/dsl/gateway/GatewayDslTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/dsl/gateway/GatewayDslTests.java @@ -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 diff --git a/spring-integration-core/src/test/java/org/springframework/integration/dsl/manualflow/ManualFlowTests.java b/spring-integration-core/src/test/java/org/springframework/integration/dsl/manualflow/ManualFlowTests.java index cee083953b..25ce10870f 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/dsl/manualflow/ManualFlowTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/dsl/manualflow/ManualFlowTests.java @@ -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 diff --git a/spring-integration-core/src/test/java/org/springframework/integration/dsl/routers/RouterTests.java b/spring-integration-core/src/test/java/org/springframework/integration/dsl/routers/RouterTests.java index 0a66e9e53a..20880ca405 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/dsl/routers/RouterTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/dsl/routers/RouterTests.java @@ -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 diff --git a/spring-integration-core/src/test/java/org/springframework/integration/handler/BridgeHandlerTests.java b/spring-integration-core/src/test/java/org/springframework/integration/handler/BridgeHandlerTests.java index 941b6cb529..78e92dc326 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/handler/BridgeHandlerTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/handler/BridgeHandlerTests.java @@ -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 request = MessageBuilder.withPayload("tst").setReplyChannel(replyChannel).build(); - handler.handleMessage(request); + this.handler.handleMessage(request); assertThat(replyChannel.receive()).matches(new MessagePredicate(request)); } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/handler/ExpressionEvaluatingMessageProcessorTests.java b/spring-integration-core/src/test/java/org/springframework/integration/handler/ExpressionEvaluatingMessageProcessorTests.java index c39da42987..d5afcbc015 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/handler/ExpressionEvaluatingMessageProcessorTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/handler/ExpressionEvaluatingMessageProcessorTests.java @@ -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 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 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 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); } diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/remote/StreamingInboundTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/remote/StreamingInboundTests.java index 30ad18b833..69475a3c1d 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/remote/StreamingInboundTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/remote/StreamingInboundTests.java @@ -17,7 +17,7 @@ package org.springframework.integration.file.remote; 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.BDDMockito.given; import static org.mockito.BDDMockito.willReturn; import static org.mockito.BDDMockito.willThrow; @@ -132,14 +132,14 @@ public class StreamingInboundTests { } @Test - public void testExceptionOnFetch() throws Exception { + public void testExceptionOnFetch() { StringSessionFactory sessionFactory = new StringSessionFactory(); Streamer streamer = new Streamer(new StringRemoteFileTemplate(sessionFactory), null); streamer.setBeanFactory(mock(BeanFactory.class)); streamer.setRemoteDirectory("/bad"); streamer.afterPropertiesSet(); - assertThatThrownBy(streamer::receive) - .isInstanceOf(MessagingException.class); + assertThatExceptionOfType(MessagingException.class) + .isThrownBy(streamer::receive); } @SuppressWarnings("unchecked") diff --git a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyCommandMessageProcessor.java b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyCommandMessageProcessor.java index cd5ced54b3..fde32934f6 100644 --- a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyCommandMessageProcessor.java +++ b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyCommandMessageProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ package org.springframework.integration.groovy; +import java.io.IOException; +import java.io.UncheckedIOException; import java.util.Map; import java.util.UUID; @@ -40,6 +42,7 @@ import groovy.lang.GString; * @author Artem Bilan * @author Stefan Reuter * @author Gary Russell + * * @since 2.0 */ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessageProcessor { @@ -59,7 +62,6 @@ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessag /** * Creates a {@link GroovyCommandMessageProcessor} that will use the provided {@link ScriptVariableGenerator}. - * * @param scriptVariableGenerator The variable generator. */ public GroovyCommandMessageProcessor(ScriptVariableGenerator scriptVariableGenerator) { @@ -72,20 +74,18 @@ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessag * and provided {@link Binding}. * Provided 'binding' will be used in the {@link BindingOverwriteGroovyObjectCustomizerDecorator} to overwrite * original Groovy Script 'binding'. - * * @param binding The binding. */ public GroovyCommandMessageProcessor(Binding binding) { - this(); Assert.notNull(binding, "binding must not be null"); this.binding = binding; } /** - * Creates a {@link GroovyCommandMessageProcessor} that will use the provided {@link ScriptVariableGenerator} and Binding. + * Creates a {@link GroovyCommandMessageProcessor} that will use the provided {@link ScriptVariableGenerator} and + * Binding. * Provided 'binding' will be used in the {@link BindingOverwriteGroovyObjectCustomizerDecorator} to overwrite * original Groovy Script 'binding'. - * * @param binding The binding. * @param scriptVariableGenerator The variable generator. */ @@ -97,7 +97,6 @@ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessag /** * Sets a {@link GroovyObjectCustomizer} for this processor. - * * @param customizer The customizer. */ public void setCustomizer(GroovyObjectCustomizer customizer) { @@ -113,26 +112,33 @@ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessag } @Override - protected Object executeScript(ScriptSource scriptSource, Map variables) throws Exception { + protected Object executeScript(ScriptSource scriptSource, Map variables) { Assert.notNull(scriptSource, "scriptSource must not be null"); - VariableBindingGroovyObjectCustomizerDecorator customizerDecorator = this.binding != null - ? new BindingOverwriteGroovyObjectCustomizerDecorator(this.binding) - : new VariableBindingGroovyObjectCustomizerDecorator(); + VariableBindingGroovyObjectCustomizerDecorator customizerDecorator = + this.binding != null + ? new BindingOverwriteGroovyObjectCustomizerDecorator(this.binding) + : new VariableBindingGroovyObjectCustomizerDecorator(); if (this.customizer != null) { customizerDecorator.setCustomizer(this.customizer); } if (!CollectionUtils.isEmpty(variables)) { customizerDecorator.setVariables(variables); } - GroovyScriptFactory factory = new GroovyScriptFactory(this.getClass().getSimpleName(), customizerDecorator); + GroovyScriptFactory factory = new GroovyScriptFactory(getClass().getSimpleName(), customizerDecorator); if (this.beanClassLoader != null) { factory.setBeanClassLoader(this.beanClassLoader); } if (this.beanFactory != null) { factory.setBeanFactory(this.beanFactory); } - Object result = factory.getScriptedObject(scriptSource); - return (result instanceof GString) ? result.toString() : result; + try { + Object result = factory.getScriptedObject(scriptSource); + return (result instanceof GString) ? result.toString() : result; + } + catch (IOException e) { + throw new UncheckedIOException(e); + } + } protected String generateScriptName(Message message) { diff --git a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessor.java b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessor.java index 1eb0ce4c9a..dbf5a2b922 100644 --- a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessor.java +++ b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessor.java @@ -16,6 +16,8 @@ package org.springframework.integration.groovy; +import java.io.IOException; +import java.io.UncheckedIOException; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Map; @@ -57,6 +59,7 @@ import groovy.transform.CompileStatic; * @author Stefan Reuter * @author Artem Bilan * @author Gary Russell + * * @since 2.0 */ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecutingMessageProcessor @@ -67,24 +70,24 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti private final Lock scriptLock = new ReentrantLock(); - private volatile ScriptSource scriptSource; + private ScriptSource scriptSource; - private volatile GroovyClassLoader groovyClassLoader = AccessController.doPrivileged( - (PrivilegedAction) () -> new GroovyClassLoader(ClassUtils.getDefaultClassLoader())); - - private volatile Class scriptClass; + private GroovyClassLoader groovyClassLoader = + AccessController.doPrivileged((PrivilegedAction) + () -> new GroovyClassLoader(ClassUtils.getDefaultClassLoader())); private boolean compileStatic; private CompilerConfiguration compilerConfiguration; + private volatile Class scriptClass; + /** * Create a processor for the given {@link ScriptSource} that will use a * DefaultScriptVariableGenerator. * @param scriptSource The script source. */ public GroovyScriptExecutingMessageProcessor(ScriptSource scriptSource) { - super(); this.scriptSource = scriptSource; } @@ -140,7 +143,7 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti } @Override - public void afterPropertiesSet() throws Exception { + public void afterPropertiesSet() { if (this.beanFactory != null && this.beanFactory instanceof ConfigurableListableBeanFactory) { ((ConfigurableListableBeanFactory) this.beanFactory).ignoreDependencyType(MetaClass.class); } @@ -155,27 +158,32 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti } @Override - protected Object executeScript(ScriptSource scriptSource, Map variables) throws Exception { + protected Object executeScript(ScriptSource scriptSource, Map variables) { Assert.notNull(scriptSource, "scriptSource must not be null"); - this.parseScriptIfNecessary(scriptSource); - Object result = this.execute(variables); + parseScriptIfNecessary(scriptSource); + Object result = execute(variables); return (result instanceof GString) ? result.toString() : result; } - private void parseScriptIfNecessary(ScriptSource scriptSource) throws Exception { + private void parseScriptIfNecessary(ScriptSource scriptSource) { if (this.scriptClass == null || scriptSource.isModified()) { - this.scriptLock.lockInterruptibly(); + this.scriptLock.lock(); try { // synchronized double check if (this.scriptClass == null || scriptSource.isModified()) { String className = scriptSource.suggestedClassName(); - if (StringUtils.hasText(className)) { - this.scriptClass = - this.groovyClassLoader.parseClass(scriptSource.getScriptAsString(), className); + try { + String scriptAsString = scriptSource.getScriptAsString(); + if (StringUtils.hasText(className)) { + this.scriptClass = this.groovyClassLoader.parseClass(scriptAsString, className); + } + else { + this.scriptClass = this.groovyClassLoader.parseClass(scriptAsString); + } } - else { - this.scriptClass = this.groovyClassLoader.parseClass(scriptSource.getScriptAsString()); + catch (IOException e) { + throw new UncheckedIOException(e); } } } @@ -210,13 +218,14 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti } catch (IllegalAccessException ex) { throw new ScriptCompilationException( - this.scriptSource, "Could not access Groovy script constructor: " + this.scriptClass.getName(), ex); + this.scriptSource, "Could not access Groovy script constructor: " + this.scriptClass.getName(), + ex); } } private final class BeanFactoryFallbackBinding extends Binding { - private BeanFactoryFallbackBinding(Map variables) { + BeanFactoryFallbackBinding(Map variables) { super(variables); } diff --git a/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/core/JpaExecutorTests.java b/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/core/JpaExecutorTests.java index a01322841f..d2161df59d 100644 --- a/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/core/JpaExecutorTests.java +++ b/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/core/JpaExecutorTests.java @@ -17,7 +17,7 @@ package org.springframework.integration.jpa.core; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.assertj.core.api.Assertions.fail; import static org.mockito.Mockito.mock; @@ -282,9 +282,9 @@ public class JpaExecutorTests { executor.setParameterSourceFactory(parameterSourceFactory); executor.setJpaParameters(Collections.singletonList(new JpaParameter("firstName", null, "#this"))); - assertThatThrownBy(executor::afterPropertiesSet) - .isExactlyInstanceOf(IllegalStateException.class) - .hasMessageStartingWith("The 'jpaParameters' and 'parameterSourceFactory' are mutually exclusive."); + assertThatIllegalStateException() + .isThrownBy(executor::afterPropertiesSet) + .withMessageStartingWith("The 'jpaParameters' and 'parameterSourceFactory' are mutually exclusive."); } } diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/util/RedisLockRegistryTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/util/RedisLockRegistryTests.java index f1e2122b43..2a3a422c74 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/util/RedisLockRegistryTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/util/RedisLockRegistryTests.java @@ -17,7 +17,7 @@ package org.springframework.integration.redis.util; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import java.util.Map; import java.util.UUID; @@ -347,9 +347,9 @@ public class RedisLockRegistryTests extends RedisAvailableTests { Lock lock1 = registry.obtain("foo"); assertThat(lock1.tryLock()).isTrue(); waitForExpire("foo"); - assertThatThrownBy(lock1::unlock) - .isInstanceOf(IllegalStateException.class) - .hasMessageContaining("Lock was released in the store due to expiration."); + assertThatIllegalStateException() + .isThrownBy(lock1::unlock) + .withMessageContaining("Lock was released in the store due to expiration."); } diff --git a/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/AbstractScriptExecutingMessageProcessor.java b/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/AbstractScriptExecutingMessageProcessor.java index 7114cb45ae..495de610ab 100644 --- a/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/AbstractScriptExecutingMessageProcessor.java +++ b/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/AbstractScriptExecutingMessageProcessor.java @@ -56,21 +56,6 @@ public abstract class AbstractScriptExecutingMessageProcessor } - /** - * Executes the script and returns the result. - */ - @Override - public final T processMessage(Message message) { - try { - ScriptSource source = this.getScriptSource(message); - Map variables = this.scriptVariableGenerator.generateScriptVariables(message); - return executeScript(source, variables); - } - catch (Exception e) { - throw IntegrationUtils.wrapInHandlingExceptionIfNecessary(message, () -> "Failed to execute script.", e); - } - } - @Override public void setBeanClassLoader(ClassLoader classLoader) { this.beanClassLoader = classLoader; @@ -81,6 +66,21 @@ public abstract class AbstractScriptExecutingMessageProcessor this.beanFactory = beanFactory; } + /** + * Executes the script and returns the result. + */ + @Override + public final T processMessage(Message message) { + try { + ScriptSource source = getScriptSource(message); + Map variables = this.scriptVariableGenerator.generateScriptVariables(message); + return executeScript(source, variables); + } + catch (Exception e) { + throw IntegrationUtils.wrapInHandlingExceptionIfNecessary(message, () -> "Failed to execute script.", e); + } + } + /** * Subclasses must implement this method to create a script source, * optionally using the message to locate or create the script. @@ -92,11 +92,10 @@ public abstract class AbstractScriptExecutingMessageProcessor /** * Subclasses must implement this method. In doing so, the execution context * for the script should be populated with the provided script variables. - *78546 @param scriptSource The script source. + * @param scriptSource The script source. * @param variables The variables. * @return The result of the execution. - * @throws Exception Any Exception. */ - protected abstract T executeScript(ScriptSource scriptSource, Map variables) throws Exception; + protected abstract T executeScript(ScriptSource scriptSource, Map variables); } diff --git a/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/jsr223/ScriptExecutingMessageProcessor.java b/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/jsr223/ScriptExecutingMessageProcessor.java index 6e86d217a1..b0e19dc593 100644 --- a/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/jsr223/ScriptExecutingMessageProcessor.java +++ b/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/jsr223/ScriptExecutingMessageProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,8 @@ import org.springframework.util.Assert; /** * @author David Turanski + * @author Artem Bilan + * * @since 2.1 */ public class ScriptExecutingMessageProcessor extends AbstractScriptExecutingMessageProcessor { @@ -40,12 +42,10 @@ public class ScriptExecutingMessageProcessor extends AbstractScriptExecutingMess /** * Create a processor for the {@link ScriptSource} using the provided * {@link ScriptExecutor} using the DefaultScriptVariableGenerator - * * @param scriptSource The script source. * @param scriptExecutor The script executor. */ public ScriptExecutingMessageProcessor(ScriptSource scriptSource, ScriptExecutor scriptExecutor) { - super(); this.scriptSource = scriptSource; this.scriptExecutor = scriptExecutor; } @@ -53,13 +53,13 @@ public class ScriptExecutingMessageProcessor extends AbstractScriptExecutingMess /** * Create a processor for the {@link ScriptSource} using the provided * {@link ScriptExecutor} - * * @param scriptSource The script source. * @param scriptVariableGenerator The script variable generator. * @param scriptExecutor The script executor. */ public ScriptExecutingMessageProcessor(ScriptSource scriptSource, ScriptVariableGenerator scriptVariableGenerator, ScriptExecutor scriptExecutor) { + super(scriptVariableGenerator); this.scriptSource = scriptSource; this.scriptExecutor = scriptExecutor; @@ -68,13 +68,13 @@ public class ScriptExecutingMessageProcessor extends AbstractScriptExecutingMess /** * Create a processor for the {@link ScriptSource} using the provided * {@link ScriptExecutor} using the DefaultScriptVariableGenerator - * * @param scriptSource The script source. * @param scriptExecutor The script executor. * @param variables The variables. */ public ScriptExecutingMessageProcessor(ScriptSource scriptSource, ScriptExecutor scriptExecutor, Map variables) { + super(new DefaultScriptVariableGenerator(variables)); this.scriptSource = scriptSource; this.scriptExecutor = scriptExecutor; @@ -87,7 +87,7 @@ public class ScriptExecutingMessageProcessor extends AbstractScriptExecutingMess } @Override - protected Object executeScript(ScriptSource scriptSource, Map variables) throws Exception { + protected Object executeScript(ScriptSource scriptSource, Map variables) { Assert.notNull(scriptSource, "scriptSource must not be null"); return this.scriptExecutor.executeScript(scriptSource, variables); } diff --git a/spring-integration-ws/src/test/java/org/springframework/integration/ws/config/UriVariableTests.java b/spring-integration-ws/src/test/java/org/springframework/integration/ws/config/UriVariableTests.java index c7fc2e2809..71dc778852 100644 --- a/spring-integration-ws/src/test/java/org/springframework/integration/ws/config/UriVariableTests.java +++ b/spring-integration-ws/src/test/java/org/springframework/integration/ws/config/UriVariableTests.java @@ -17,7 +17,7 @@ package org.springframework.integration.ws.config; 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.BDDMockito.willThrow; import static org.mockito.Mockito.doAnswer; @@ -51,6 +51,7 @@ import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHandler; import org.springframework.messaging.MessageHandlingException; +import org.springframework.messaging.MessagingException; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.ws.client.WebServiceClientException; @@ -130,8 +131,9 @@ public class UriVariableTests { .setHeader("param", "test1 & test2") .build(); - assertThatThrownBy(() -> this.inputHttp.send(message)) - .hasCauseInstanceOf(WebServiceIOException.class); // offline + assertThatExceptionOfType(MessagingException.class) + .isThrownBy(() -> this.inputHttp.send(message)) + .withCauseInstanceOf(WebServiceIOException.class); // offline assertThat(uri.get()).isEqualTo("http://localhost/spring-integration?param=test1%20&%20test2"); } diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/DefaultXmlPayloadConverterTests.java b/spring-integration-xml/src/test/java/org/springframework/integration/xml/DefaultXmlPayloadConverterTests.java index 0e9c6ba635..c3f5a7cd53 100644 --- a/spring-integration-xml/src/test/java/org/springframework/integration/xml/DefaultXmlPayloadConverterTests.java +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/DefaultXmlPayloadConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,7 @@ package org.springframework.integration.xml; 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 java.io.File; import java.io.IOException; @@ -117,8 +117,8 @@ public class DefaultXmlPayloadConverterTests { @Test public void testInvalidPayload() { - assertThatThrownBy(() -> converter.convertToSource(12)) - .isExactlyInstanceOf(MessagingException.class); + assertThatExceptionOfType(MessagingException.class) + .isThrownBy(() -> converter.convertToSource(12)); } @Test diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/source/DomSourceFactoryTests.java b/spring-integration-xml/src/test/java/org/springframework/integration/xml/source/DomSourceFactoryTests.java index 325e045a5e..5ef23d6856 100644 --- a/spring-integration-xml/src/test/java/org/springframework/integration/xml/source/DomSourceFactoryTests.java +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/source/DomSourceFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,7 @@ package org.springframework.integration.xml.source; 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 java.io.StringReader; @@ -74,8 +74,8 @@ public class DomSourceFactoryTests { @Test public void testWithUnsupportedPayload() { - assertThatThrownBy(() -> sourceFactory.createSource(12)) - .isExactlyInstanceOf(MessagingException.class); + assertThatExceptionOfType(MessagingException.class) + .isThrownBy(() -> sourceFactory.createSource(12)); } } diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/source/StringSourceTests.java b/spring-integration-xml/src/test/java/org/springframework/integration/xml/source/StringSourceTests.java index 57957283fc..c33362f5da 100644 --- a/spring-integration-xml/src/test/java/org/springframework/integration/xml/source/StringSourceTests.java +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/source/StringSourceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,7 @@ package org.springframework.integration.xml.source; 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 java.io.BufferedReader; @@ -65,8 +65,8 @@ public class StringSourceTests { public void testWithUnsupportedPayload() { String docString = "one"; StringBuffer buffer = new StringBuffer(docString); - assertThatThrownBy(() -> sourceFactory.createSource(buffer)) - .isExactlyInstanceOf(MessagingException.class); + assertThatExceptionOfType(MessagingException.class) + .isThrownBy(() -> sourceFactory.createSource(buffer)); } } diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/ResultToStringTransformerTests.java b/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/ResultToStringTransformerTests.java index 7a49d2b8cb..ec2199f4f8 100644 --- a/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/ResultToStringTransformerTests.java +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/ResultToStringTransformerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,7 @@ package org.springframework.integration.xml.transformer; 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 java.util.Properties; @@ -83,8 +83,8 @@ public class ResultToStringTransformerTests { @Test public void testWithUnsupportedSaxResult() { - assertThatThrownBy(() -> this.transformer.transformResult(new SAXResult())) - .isExactlyInstanceOf(MessagingException.class); + assertThatExceptionOfType(MessagingException.class) + .isThrownBy(() -> this.transformer.transformResult(new SAXResult())); } } diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTests.java b/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTests.java index 4802e5ee59..1884b854fb 100644 --- a/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTests.java +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTests.java @@ -17,7 +17,8 @@ package org.springframework.integration.xml.transformer; 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.assertThatIllegalStateException; import java.io.File; import java.io.IOException; @@ -163,21 +164,21 @@ public class XsltPayloadTransformerTests { XsltPayloadTransformer transformer = new XsltPayloadTransformer(getXslResourceThatOutputsText(), "foo.bar.Baz"); transformer.setBeanFactory(Mockito.mock(BeanFactory.class)); - assertThatThrownBy(transformer::afterPropertiesSet) - .isExactlyInstanceOf(IllegalStateException.class) - .hasCauseExactlyInstanceOf(ClassNotFoundException.class); + assertThatIllegalStateException() + .isThrownBy(transformer::afterPropertiesSet) + .withCauseExactlyInstanceOf(ClassNotFoundException.class); } @Test public void testNonXmlString() { - assertThatThrownBy(() -> this.transformer.doTransform(new GenericMessage<>("test"))) - .isExactlyInstanceOf(TransformerException.class); + assertThatExceptionOfType(TransformerException.class) + .isThrownBy(() -> this.transformer.doTransform(new GenericMessage<>("test"))); } @Test public void testUnsupportedPayloadType() { - assertThatThrownBy(() -> this.transformer.doTransform(new GenericMessage<>(12))) - .isExactlyInstanceOf(MessagingException.class); + assertThatExceptionOfType(MessagingException.class) + .isThrownBy(() -> this.transformer.doTransform(new GenericMessage<>(12))); } @Test