2016-08-19 77 views
6

我在爲使用Spring Integration DSL的IntegrationFlow編寫測試用例時遇到了麻煩。下面是我的代碼片段,我想測試'變換'部分。請提供一些幫助,用嘲諷的手柄部分或者有一些其他的方式來測試這一點 - 我們沒有mocking framework尚未IntegrationTests for Spring IntegrationFlow

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

,但確實是在不久的將來打算。

你可以考慮使用@MockBean從最新的Spring Boot爲你的HttpRequestExecutingMessageHandler httpMessageHandlerSpec來替換那個bean與所需的模擬。

另一方面,您可以直接發送消息到input channel爲您的.transform。請從語閱讀手冊:

默認情況下端點通過DirectChannel其中bean名稱是基於模式的接線方式:[IntegrationFlow.beanName] .channel#[channelNameIndex。

所以,在你需要的流動通道有豆的名字,如:dmwGetProductDetailsByEan.channel#0,因爲這是你的IntegrationFlow定義中第一個未命名的頻道。

相關問題