Handling errors

So far so good, how about handling error scenarios? How to test negative use cases and what to do with the failed test case? The following code snippet will help us in these matters:

  @Test(expected = MessageTransformationException.class)
  public void errorReportedWhenPayloadIsWrong() {
    Message<String> message = 
      MessageBuilder.withPayload("this should fail").build();
    filteredFeedChannel.send(message);
  }

The input channel expects a message with the payload type SyndEntry, but if a message with a String payload is sent—this must throw an exception. This is what has been tested. This can be further enhanced to monitor certain types of exception on channels with the ability to validate user-defined propagated messages.

Get Spring Integration Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.