spring 集成程序的单元测试问题

Problems with unit testing a spring integration program

我正在尝试对 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 条不同的路线(散装、运输)+ 错误 channel.Here 是我的运输通道路线的单元测试用例:

        @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 上下文文件中,我将事务通道连接到测试 channel.in junit 测试用例,我在 junit 方法中向路由器发送有效负载,并尝试从输入通道接收它并将其用于断言。但是断言失败,因为来自事务通道的消息在路由到 junit 上下文文件中给出的 inputChannel 之前直接转到 consoleOut。如何在消息进入 consoleOut 之前拦截消息?我也尝试添加 wireTap 拦截器,但它们没有用:

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

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

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

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