How to unit test Spring IntegrationFlow?

微笑、不失礼 提交于 2020-01-03 19:07:10

问题


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!


回答1:


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.




回答2:


Seems for me "unit testing" means check the behavior of the particular part of the system, some small component.

So, in your case it is about that new GenericTransformer.

so, just make it as a top-level component and perform tests against its isolated instances!

The integration tests can be performed against the target IntegrationFlow as well.

Each EIP-component in the flow definition is surrounded with MessageChannels - input and output. Even if you don't declare .channel() there, the Framework build implicit DirrectChannel to wire endpoints to the flow.

Those implicit get the bean name like:

channelBeanName = flowNamePrefix + "channel" +
                                BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR + channelNameIndex++;

So, since your IntegrationFlow is from Lambda, the input channel form the .transform() is just input of the flow - transformMessage.input.

The channel between .transform() and the next .handle() has bean name like: transformMessage.channel#0, because it will be a first implicit channel declaration.

The idea that you can @Autowired both of this channels to your test-case and add ChannelInterceptor to them before testing.

The ChannelInterceptor may play verificator role to be sure that you send to the transformer and receive from the a proper data as it is expected.

More info can be found here: https://github.com/spring-projects/spring-integration-java-dsl/issues/23




回答3:


Example of DSL IntegrationFlows testing is on github.



来源:https://stackoverflow.com/questions/42007080/how-to-unit-test-spring-integrationflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!