Search code examples
junitspring-integrationdslspring-annotations

How to unit test Spring IntegrationFlow?


I have been using Spring Integration DSL to implement some messaging processing flow.

How can I actually unit test a single IntegrationFlow, can anyone provide me with an example on how to unit test i.e. transform part of this bean:

@Bean
public IntegrationFlow transformMessage(){      
    return message -> message               
            .transform(new GenericTransformer<Message<String>, Message<String>>() {
                @Override
                public Message<String> transform(Message<String> message) {

                    MutableMessageHeaders headers = 
                          new MutableMessageHeaders(message.getHeaders());
                    headers.put("Content-Type", "application/json");
                    headers.put("Accept", "application/json");                      

                    String payload = "Long message";
                    ObjectMapper mapper = new ObjectMapper();

                    HashMap<String, String> map = new HashMap<>();
                    map.put("payload", payload);

                    String jsonString = null;
                    try {
                        jsonInString = mapper.writeValueAsString(map);
                    } catch (JsonProcessingException e) {
                        logger.error("Error:" + e.getMessage());                            
                    }

                    Message<String> request = new GenericMessage<String>(jsonString
                    , headers);                                     
                    return request;                 
                }
            })
            .handle(makeHttpRequestToValidateAcdrMessage())                                                     
            .enrichHeaders(h -> h.header("someHeader", "blah", true))
            .channel("entrypoint");
}

How can I test it?

Regards!


Solution

  • The same techniques described in the testing-samples project in the samples repo can be used here.

    The send a message to channel transform.input and subscribe to entrypoint to get the result (or change it to a QueueChannel in your test case.