apache camel单元测试模拟副作用

时间:2017-02-15 11:12:45

标签: java junit apache-camel

我在课程Camel in Action中预订FirstMockTest的示例中提供了以下代码:

String msg1 = "Camel Msg1";
String msg2 = "Camel Msg2";
String msg3 = "Camel Msg3";
String msg4 = "Camel Msg4";

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("jms:topic:quote").to("mock:quote");
            from("jms:topic:quote2").to("mock:quote2");
        }
    };
}
@Override
protected CamelContext createCamelContext() throws Exception {
    CamelContext context = super.createCamelContext();
    //fake jms register
    context.addComponent("jms", context.getComponent("seda"));
    return context;
}


@Test
public void testMultipleBodies() throws InterruptedException {
    MockEndpoint quote = getMockEndpoint("mock:quote");
    template.sendBody("jms:topic:quote", msg1);
    template.sendBody("jms:topic:quote", msg2);
    quote.expectedBodiesReceived(msg1, msg2);
    quote.assertIsSatisfied();
}
@Test
public void testMultipleBodies2() throws InterruptedException {
    MockEndpoint quote2 = getMockEndpoint("mock:quote2");
    template.sendBody("jms:topic:quote2", msg3);
    template.sendBody("jms:topic:quote2", msg4);
    quote2.expectedBodiesReceived(msg3, msg4);
    quote2.assertIsSatisfied();
}

运行gradle clean test --tests FirstMockTest时,我得到以下内容 单元测试错误输出:

 FirstMockTest > testMultipleBodies2 FAILED
    java.lang.AssertionError: mock://quote2 Body of message: 0. 
Expected: <Camel Msg3> but was: <Camel Msg4>

删除testMultipleBodies使测试不会失败 并更改template.sendBody命令给出    ....Expected: <Camel Msg4> but was: <null>

所以配置中的两条路线似乎没有分开?

版本: apache-camel:2.5.0 Junit:4.11

1 个答案:

答案 0 :(得分:0)

两条JMS路由可以同时处理,因此根据CPU时序等消息可以以不同的顺序到达模拟端点。

所以应该从

改变
quote2.expectedBodiesReceived(msg3, msg4);

quote2.expectedBodiesReceivedInAnyOrder(msg3, msg4);