6
votes

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();
    }
}
1

1 Answers

1
votes

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.