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

时间:2015-10-28 01:35:59

标签: spring unit-testing spring-integration

我正在尝试对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' " />   

正如你在这里看到的,有两种不同的路径(批量,反式)+错误通道。这是我的跨频道路由单元测试用例:

        @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);

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

1 个答案:

答案 0 :(得分:0)

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

您可以在发送邮件之前,通过id EventDrivenConsumerstop()自动对其进行自动装配,以取消订阅测试用例。