简体   繁体   中英

Problems with unit testing a spring integration program

I am trying to unit test an xpath router, but having problems with it. here is my context file:

   <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' " />   

As you can see here, there are 2 different routes(bulk,trans.) + error channel.Here is my unit test case for trans channel route:

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

context file for junit

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

As you can see, in the junit context file, I am connecting transactional channel to the test channel.in the junit test case, I am sending a payload to the router in the junit method, and trying to receive it from the input channel and use it for assertion. however the assertion fails, as the message from transaction channel directly goes to consoleOut before getting routed to inputChannel as given in the junit coontext file. How do I intercept the message before it goes to consoleOut? I also tried adding wireTap interceptors but they didnt work:

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

Basically, I need a separate flow for unit testing.

With that configuration, you are just adding a second consumer to transactionChannel - messages will be round-robin distributed to the transformer and bridge.

You can unsubscribe the transformer for your test case by autowiring it by id as an EventDrivenConsumer and stop() it before sending your message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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