Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntegrationTests for Spring IntegrationFlow

I am having trouble in writing test cases for my IntegrationFlow which is using Spring Integration DSL. Below is my code snippet and I would like to test the 'transform' part. Please provide some help in mocking the handle part or if there is some other way to test this -

public class DmwConfig {
    @Value("${dmw.url.hostname}")
    public String hostName;

    @Bean
    public MessageChannel dmwGetProductDetailsByEanChannel() {
        return MessageChannels.direct().get();
    }

    @Bean
    public IntegrationFlow dmwGetProductDetailsByEan() {
        return IntegrationFlows
                .from("input")
                .channel("dmwGetProductDetailsByEanChannel")
                .handle(httpMessageHandlerSpec())
                .<JsonNode, ProductModel>transform(
                        node -> new ProductModel(
                            node.findValue("name").asText(null),
                            node.findValue("inventory").findValue("orderable").asBoolean(false),
                            node.findValue("stock_level").asInt(0),
                            node.findValue("price").asDouble(0),
                            "", // this url field will be enriched in the controller because the url doesn't contain any data from the response
                            node.findValue("image_groups").findValue("link").asText(null)
                        )
                )
                .get();
    }

    @Bean
    public HttpRequestExecutingMessageHandler httpMessageHandlerSpec() {
        return Http
                .outboundGateway((Message<DmwPayload> p) -> "foobar url")
                .charset("UTF-8")
                .httpMethod(HttpMethod.GET)
                .expectedResponseType(JsonNode.class).get();
    }
}
like image 334
Ratheesh K Avatar asked Aug 19 '16 07:08

Ratheesh K


People also ask

What is IntegrationFlow in spring?

Interface IntegrationFlow This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

How can I initialize a spring Applicationcontext just once for all tests?

Is there a way to make sure the spring context is initialized only once? Use a test suite. You can put the @RunWith , @TestExecutionListeners @ContextConfiguration annotations on your base class, and this will just initialise the context once.


1 Answers

We don't have mocking framework yet, but that is really intention in the nearest future.

You can consider to use @MockBean from the latest Spring Boot for your HttpRequestExecutingMessageHandler httpMessageHandlerSpec to replace that bean with desired mock.

On the other hand you can just send message directly to the input channel for that your .transform. Please, read that manual from the phrase:

By default endpoints are wired via DirectChannel where the bean name is based on the pattern: [IntegrationFlow.beanName].channel#[channelNameIndex].

So, required channel in your flow has bean name like: dmwGetProductDetailsByEan.channel#0, because this is a first unnamed channel in your IntegrationFlow definition.

like image 146
Artem Bilan Avatar answered Oct 18 '22 02:10

Artem Bilan