Apache camel 测试驼峰路线时如何正确使用模拟?

Apache camel 测试驼峰路线时如何正确使用模拟?,apache-camel,Apache Camel,我正在尝试编写一个Camel测试,检查基于内容的路由器是否正确路由XML文件。以下是my blueprint.xml中的点和路线: <endpoint uri="activemq:queue:INPUTQUEUE" id="jms.queue.input" /> <endpoint uri="activemq:queue:QUEUE1" id="jms.queue.1" /> <endpoint uri="activemq:queue:QUEUE2" id="jms

我正在尝试编写一个Camel测试,检查基于内容的路由器是否正确路由XML文件。以下是my blueprint.xml中的点和路线:

<endpoint uri="activemq:queue:INPUTQUEUE" id="jms.queue.input" />
<endpoint uri="activemq:queue:QUEUE1" id="jms.queue.1" />
<endpoint uri="activemq:queue:QUEUE2" id="jms.queue.2" />

<route id="general-jms.to.specific-jms">
    <from ref="jms.queue.input" />
    <choice>
      <when>
        <xpath>//object-type = '1'</xpath>
        <log message="Sending message to queue: QUEUE1" />
        <to ref="jms.queue.1" />
      </when>
      <when>
        <xpath>//object-type = '2'</xpath>
        <log message="Sending message to queue: QUEUE2" />
        <to ref="jms.queue.2" />
      </when>
      <otherwise>
        <log message="No output was able to be determined based on the input." />
      </otherwise>
    </choice>
</route>
当我运行此测试时,我看到我放在路由定义中的日志消息,该消息表示它正在将其发送到QUEUE1,但JUnit测试失败,并显示以下错误消息:java.lang.AssertionError:mock://activemq:queue:QUEUE1 收到的邮件数。应为:但为:

有人能帮我理解我做错了什么吗

我的理解是,Camel将自动模拟QUEUE1端点,因为我重写了
isMockEndpointsAndSkip()
,并提供了QUEUE1端点uri。我认为这意味着我应该能够在
getMockEnpoint()
方法中使用该端点,只需在uri的开头添加“mock:”。然后我应该有一个模拟的端点,我可以对其设置期望值(即,必须有输入文件)


如果我对某些事情不清楚,请让我知道,并非常感谢任何帮助

在做了一段时间之后,我想到的唯一一个真正适合我的解决方案是在我的测试类中使用
createRouteBuilder()
方法,将路由添加到blueprint.xml文件中定义的路由末尾的模拟端点。然后我可以检查模拟的端点是否符合我的期望。下面是我测试类的最终代码。蓝图XML保持不变

public class RouteTest extends CamelBlueprintTestSupport {

@Override
protected String getBlueprintDescriptor() {
    return "/OSGI-INF/blueprint/blueprint.xml";
}

@Test
public void testQueue1Route() throws Exception {

    getMockEndpoint("mock:QUEUE1").expectedBodiesReceived(context.getTypeConverter().convertTo(String.class, new File("src/test/resources/queue1-test.xml")));

    template.sendBody("activemq:queue:INPUTQUEUE", context.getTypeConverter().convertTo(String.class, new File("src/test/resources/queue1-test.xml")));

    assertMockEndpointsSatisfied();
}

@Test
public void testQueue2Route() throws Exception {

    getMockEndpoint("mock:QUEUE2").expectedBodiesReceived(context.getTypeConverter().convertTo(String.class, new File("src/test/resources/queue2-test.xml")));

    template.sendBody("activemq:queue:INPUTQUEUE", context.getTypeConverter().convertTo(String.class, new File("src/test/resources/queue2-test.xml")));

    assertMockEndpointsSatisfied();
}

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {
        public void configure() throws Exception {
            from("activemq:queue:QUEUE1").to("mock:QUEUE1");
            from("activemq:queue:QUEUE2").to("mock:QUEUE2");
        }
    };
}
}

虽然此解决方案可行,但我不完全理解为什么我不能只使用
isMockEndpointsAndSkip()
,而不必在现有blueprint.xml路由的末尾手动定义新路由。我的理解是,使用
return“*”定义
isMockEndpointsAndSkip()
将为blueprint.xml文件中定义的所有端点注入模拟端点。然后,您可以检查这些模拟端点上的期望值。但由于某些原因,这对我不起作用。

解决方案是使用

此方法完全没有任何文档,但在这样调用它时,它对我有效:

public class FooTest extends CamelTestSupport {

    @Override
    public void setUp() throws Exception {
        replaceRouteFromWith("route-id", "direct:route-input-replaced");
        super.setUp();
    }

    // other stuff ...

}

这也将防止
的原始使用者从路线的
目的地启动。例如,这意味着在测试带有activemq使用者的路由时,不再需要运行activemq实例。

Jon,我制作了一个包含内容
1
的文件
queue1 test.xml
,该测试对我有效(在等效的java dsl中,而不是xml路由配置中)。您的输入文件看起来怎么样?我的XML只包含
1
。我认为,如果将其定义为XML,就会出现一些奇怪的情况。如果路由是用XML定义的,那么
isMockEndpointsAndSkip()
方法似乎不起作用。这可能与模拟ActiveMQ端点有关。这篇文章似乎已经使它能够与XML一起工作:(查看驼峰路线和utest标题下)。不用担心,我想这是您的代码和我的代码之间的主要区别。我习惯性地使用JavaDSL,有编译时语法检查和自动完成很好。值得考虑吗?这是使用JavaDSL的两个优点。我去看看。谢谢你的帮助!感谢这位太空卡车司机。我在寻找类似的东西。这解决了我的问题。
public class FooTest extends CamelTestSupport {

    @Override
    public void setUp() throws Exception {
        replaceRouteFromWith("route-id", "direct:route-input-replaced");
        super.setUp();
    }

    // other stuff ...

}