【问题标题】:IntegrationTests for Spring IntegrationFlowSpring IntegrationFlow 的集成测试
【发布时间】:2016-08-19 07:43:03
【问题描述】:

我在为使用 Spring Integration DSL 的 IntegrationFlow 编写测试用例时遇到问题。下面是我的代码 sn-p,我想测试“转换”部分。请提供一些帮助来模拟手柄部分,或者是否有其他方法可以测试 -

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

【问题讨论】:

    标签: spring-integration


    【解决方案1】:

    我们还没有mocking framework,但这确实是近期的打算。

    您可以考虑为您的HttpRequestExecutingMessageHandler httpMessageHandlerSpec 使用最新的Spring Boot 中的@MockBean 以用所需的模拟替换该bean。

    另一方面,您可以直接向input channel 发送消息,以获取您的.transform。请从以下短语中阅读该手册:

    默认情况下,端点通过 DirectChannel 连接,其中 bean 名称基于以下模式:[IntegrationFlow.beanName].channel#[channelNameIndex]。

    因此,您的流程中所需的通道具有 bean 名称,例如:dmwGetProductDetailsByEan.channel#0,因为这是您的 IntegrationFlow 定义中的第一个未命名通道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-19
      • 1970-01-01
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多