Spring Integration:如果一起执行,测试方法会失败

时间:2012-11-13 14:42:55

标签: spring unit-testing spring-integration

我的应用程序的工作流程是添加邮件标头,然后根据另一个标头路由邮件并在下游发送邮件。我对这个工作流程的测试单独成功,但在一起执行时却没有成功,尽管我似乎已经重置了所有相关对象。

弹簧集成工作流程:

<int:header-enricher input-channel="workflowStart" output-channel="headerEnriched">
    <int:header name="correlationId" expression="headers.id.toString()" />
</int:header-enricher>

<int:header-value-router input-channel="headerEnriched" header-name="messageType">
    <int:mapping value="stp" channel="stpChannel" />
    <int:mapping value="nonStp" channel="nonStpChannel" />
</int:header-value-router>

<int:publish-subscribe-channel id="nonStpChannel"   />
<int:publish-subscribe-channel id="stpChannel" apply-sequence="true"/>

<int:chain input-channel="nonStpChannel" output-channel="systemChannel1" id="nonStpChainBlockA"> 
    <!-- do something -->
</int:chain> 

<int:chain input-channel="stpChannel" output-channel="systemChannel3" id="stpChainBlockA"> 
    <!-- do something -->
</int:chain> 

Java测试类:

@Autowired 
    private MessageChannel workflowStart;
    @Autowired
    private MessageHandler systemChannel1OutputHandler; 
    @Autowired
    private MessageHandler systemChannel3OutputHandler; 

    @Value("/xml/tradeStp.xml")
    private Resource stpMessageResource;
    private String stpMessage;
    @Value("/xml/tradeNonStp.xml")
    private Resource nonStpMessageResource;
    private String nonStpMessage;


    @Before
    public void setup() throws Exception {
        reset(systemChannel1OutputHandler);
        reset(systemChannel3OutputHandler);
        stpMessage = IOUtils.toString(stpMessageResource.getInputStream());
        nonStpMessage = IOUtils.toString(nonStpMessageResource.getInputStream());
    }

    @Test
    public void nonStpMessageWorkflow1Test() {
        Message<String> m = MessageBuilder
                .withPayload(nonStpMessage)
                .setHeaderIfAbsent("messageType", "nonStp")
                .build();       
        workflowStart.send(m);
        verify(systemChannel1OutputHandler, times(1)).handleMessage(any(Message.class));    
    }

    @Test
    public void stpMessageWorkflow2Test() {
        Message<String> m = MessageBuilder
                .withPayload(stpMessage)
                .setHeaderIfAbsent("messageType", "stp")
                .build();
        workflowStart.send(m);
        verify(systemChannel3OutputHandler, times(1)).handleMessage(any(Message.class));
    }

测试环境:

<import resource="classpath:/spring/workflow.xml"/>

<int:outbound-channel-adapter channel="systemChannel1" ref="systemChannel1OutputHandler" method="handleMessage"/>
<bean id="systemChannel1OutputHandler" class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="org.springframework.integration.core.MessageHandler"/>
</bean> 
<int:outbound-channel-adapter channel="systemChannel3" ref="systemChannel3OutputHandler" method="handleMessage"/>
<bean id="systemChannel3OutputHandler" class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="org.springframework.integration.core.MessageHandler"/>
</bean>

<int:outbound-channel-adapter channel="headerEnriched" ref="headerEnricherOutputHandler" method="handleMessage"/>
<bean id="headerEnricherOutputHandler" class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="org.springframework.integration.core.MessageHandler"/>
</bean>

如果我同时运行两个测试,第一个测试成功,而第二个测试失败并显示消息:

    Wanted but not invoked:
messageHandler.handleMessage(<any>);
-> at com.company.integration.com.company.export.config.InterfaceTest.stpMessageWorkflow2Test(InterfaceTest.java:81)
Actually, there were zero interactions with this mock.

我试图以各种方式进行调试。问题似乎是在header-value-router之后和链之前。第二个测试的最后一个调试输出是headerEnriched通道上的“postSend”。 任何建议都非常欢迎。

更新: 我没有在我的上下文中包含一个额外的outputHandler,用于另一个测试,我现在已经包含了。通过删除headerEnricherOutputHandler,所有测试运行正常。但是,我仍然不明白在测试一起运行时这会导致问题的原因。

2 个答案:

答案 0 :(得分:0)

我使用任何(类)的Mockito都遇到了问题。你在哪里

verify(systemChannel3OutputHandler, times(1)).handleMessage(any(Message.class));

如果删除该类,它是否可以使用,例如:

verify(systemChannel3OutputHandler, times(1)).handleMessage(any());

答案 1 :(得分:0)

我无法通过以下方式重现您的问题,这与您的测试没有显着差异......

<int:header-value-router input-channel="headerEnriched" header-name="messageType">
    <int:mapping value="stp" channel="stpChannel" />
    <int:mapping value="nonStp" channel="nonStpChannel" />
</int:header-value-router>

<int:publish-subscribe-channel id="nonStpChannel"   />
<int:publish-subscribe-channel id="stpChannel" apply-sequence="true"/>

<int:chain input-channel="stpChannel" output-channel="systemChannel1">
    <int:transformer expression="'bar'"/>
</int:chain>

<int:chain input-channel="nonStpChannel" output-channel="systemChannel3">
    <int:transformer expression="'bar'"/>
</int:chain>

<int:channel id="systemChannel1" />
<int:channel id="systemChannel3" />

<int:outbound-channel-adapter channel="systemChannel1" ref="systemChannel1OutputHandler" method="handleMessage"/>
<bean id="systemChannel1OutputHandler" class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="org.springframework.integration.core.MessageHandler"/>
</bean> 
<int:outbound-channel-adapter channel="systemChannel3" ref="systemChannel3OutputHandler" method="handleMessage"/>
<bean id="systemChannel3OutputHandler" class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="org.springframework.integration.core.MessageHandler"/>
</bean>

@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class Foo {

    @Autowired
    private MessageChannel headerEnriched;

    @Autowired
    private MessageHandler systemChannel1OutputHandler;

    @Autowired
    private MessageHandler systemChannel3OutputHandler;

    @Test
    public void test10() {
        Message<String> foo = MessageBuilder.withPayload("foo")
                .setHeader("messageType", "stp").build();
        headerEnriched.send(foo);
        verify(systemChannel1OutputHandler).handleMessage(any(Message.class));
    }

    @Test
    public void test30() {
        Message<String> foo = MessageBuilder.withPayload("foo")
                .setHeader("messageType", "nonStp").build();
        headerEnriched.send(foo);
        verify(systemChannel3OutputHandler).handleMessage(any(Message.class));
    }
}

这两项测试都适合我。