I have a following flow implemented in Spring Integration DSL:
- Take feed from HTTP
- Enrich errorChannel header (point of handling all exception downstream here).
- Transform message to be a Collection
- Split Collection into sepearate messages
- Send each message to next processing channels
@Bean
public IntegrationFlow inboundHttpFlow(
Puller puller,
HeaderEnricher errorHandlingChannelHeaderEnricher,
FeedTransformer feedTransformer,
MessageChannel outputFeedIntegrationChannel
) {
final Consumer<SourcePollingChannelAdapterSpec> pollingSpec = spec ->
spec
.poller(Pollers.cron(SCHEDULE_EVERY_HALF_MINUTE)
.errorChannel(INBOUND_ERROR_CHANNEL));
return IntegrationFlows
.from(puller, pollingSpec)
.enrichHeaders(errorHandlingChannelHeaderEnricher)
.transform(feedTransformer)
.split()
.channel(outputFeedIntegrationChannel)
.get();
}
Where my errorHandlingChannelHeaderEnricher is:
@Component
public class ErrorHandlingChannelHeaderEnricher implements HeaderEnricher {
@Override
public void accept(HeaderEnricherSpec spec) {
spec.header(
MessageHeaders.ERROR_CHANNEL,
INBOUND_ERROR_CHANNEL,
true
);
}
}
When feedTransformer throws an exception in working app then it goes to set errorChannel as expected. But I don't know how to write a test to test if thrown exception goes to errorChannel defined in header?
When I'm trying to simulate it in test given way, it doesn't work because exception is thrown back into caller instead of errorChannel:
// given
Throwable transformerException = new IllegalStateException();
when(feedTransformerMock.apply(any())).thenThrow(transformerException);
// when
var testFeedMessage = MessageBuilder
.withPayload(pullerResult)
.build();
inboundHttpFlow.getInputChannel().send(testFeedMessage); // excetpion returns to caller here
// then
verify(errorHandlerSpy).accept(transformerException);
And exception is typical:
org.springframework.integration.transformer.MessageTransformationException: Failed to transform Message; nested exception is org.springframework.messaging.MessageHandlingException: nested exception is java.lang.IllegalStateException, failedMessage=GenericMessage [payload=test-payload, headers={errorChannel=inboundErrorChannel, id=f77a6a01-9bca-5af3-8352-7edb4c5e94b0, timestamp=1556019833867}]
, failedMessage=GenericMessage [payload=test-payload, headers={errorChannel=inboundErrorChannel, id=f77a6a01-9bca-5af3-8352-7edb4c5e94b0, timestamp=1556019833867}]
I assume that because of DirectChannel and lack of poller in this test example in compare to real flow (with poller). Is there any way to simulate that throwing exception and checking if it really goes to errorChannel defined in header?