【问题标题】:Problems with unit testing a spring integration program单元测试弹簧集成程序的问题
【发布时间】:2015-10-28 01:35:59
【问题描述】:

我正在尝试对 xpath 路由器进行单元测试,但遇到了问题。这是我的上下文文件:

   <int:channel id="toTransactionTypeRouterChannel" />  
   <int-xml:xpath-router id="transactionsTypeRouter"
    input-channel="toTransactionTypeRouterChannel" resolution-required="false"
    evaluate-as-string="true" default-output-channel="errorChannel">
    <!-- Select node name of the first child -->
    <int-xml:xpath-expression
        expression="name(/soapNs:Envelope/soapNs:Body/schNs:processArchiveRequest/schNs:fulfillmentRequest/schNs:requestDetail/*[1])"
        namespace-map="archiveNamespaceMap" />
    <int-xml:mapping value="sch:bulkRequestDetail"
        channel="bulkChannel" />
    <int-xml:mapping value="sch:transactionalRequestDetail"
        channel="transactionChannel" />
</int-xml:xpath-router>

<int:channel id="bulkChannel" />
<int:channel id="transactionChannel" />

<int:transformer input-channel="bulkChannel"
    output-channel="consoleOut" expression="'Bulk channel has received the payload' " />
<int:transformer input-channel="transactionChannel"
    output-channel="consoleOut" expression="'Transaction channel has received payload' " />
<int:transformer input-channel="errorChannel"
    output-channel="consoleOut" expression="'Error channel has received payload' " />   

正如您在此处看到的,有 2 条不同的路由(散装,传输。)+ 错误通道。这是我的跨通道路由的单元测试用例:

        @Test
        public void testTransactionFlow() throws Exception {
    try {


        Resource bulkRequest = new FileSystemResource("src/main/resources/mock-message-examples/SampleProcessArchiveTransRequest.xml");

          String transRequestStr= extractResouceAsString(bulkRequest);

        toTransactionTypeRouterChannel.send(MessageBuilder.withPayload(transRequestStr).build());   
        Message<?> outMessage = testChannel.receive(0);
        assertNotNull(outMessage);

junit 的上下文文件

    <int:bridge input-channel="transactionChannel"
    output-channel="testChannel"/>
    <int:channel id="testChannel">
    <int:queue/>
</int:channel>

如您所见,在 junit 上下文文件中,我将事务通道连接到测试通道。在 junit 测试用例中,我在 junit 方法中向路由器发送有效负载,并尝试从输入通道并将其用于断言。但是断言失败,因为来自事务通道的消息在被路由到 junit 上下文文件中给出的 inputChannel 之前直接转到 consoleOut。如何在消息发送到 consoleOut 之前拦截消息?我也尝试添加窃听器拦截器,但它们没有工作:

     WireTap  wireTap = new WireTap(someChannel);
        boolean w = wireTap.isRunning();        
        transactionChannel.addInterceptor(wireTap);

基本上,我需要一个单独的单元测试流程。

【问题讨论】:

    标签: spring unit-testing spring-integration


    【解决方案1】:

    使用该配置,您只需向transactionChannel 添加第二个消费者 - 消息将循环分发到转换器和网桥。

    在发送消息之前,您可以通过 id 将其自动装配为 EventDrivenConsumerstop() 来取消订阅您的测试用例的转换器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多