繁体   English   中英

弹簧集成程序的单元测试问题

[英]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条不同的路由(批量,传输)+错误通道。这是我的跨通道路由的单元测试用例:

        @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 coontext文件中给定的路由到inputChannel之前直接进入consoleOut。 在进入consoleOut之前如何截获消息? 我还尝试添加wireTap拦截器,但它们没有起作用:

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

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

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

您可以通过以id作为EventDrivenConsumer自动装配它并在发送消息之前对其进行stop()来取消订阅该测试用例的转换器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM