0

我正在尝试为骆驼路线设置测试。我的测试路由读取一个二进制文件并将其发送到一个翻译器 bean,返回一个 POJO。现在,我想对 POJO 做一些断言,以确保那里的值与已知值匹配。我认为标准的东西。在我看到的示例中,body 似乎总是一个 String 或原始类型,并且可以对其进行简单的断言。但是,就我而言,它是一个对象,所以我想以某种方式获取该对象。

这是我到目前为止所尝试的:

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("file:///path/to/dir/of/test/file/?noop=true")
            .bean(TranslatorBean.class)
            .to("mock:test").end();
        }
    };
}

@Test
public void testReadMessage() throws Exception {

    MockEndpoint endpoint = getMockEndpoint("mock:test");

    endpoint.whenAnyExchangeReceived(new Processor() {      
        @Override
        public void process(Exchange exchange) throws Exception {           
            Object body = exchange.getIn().getBody();

            assertIsInstanceOf(MyPOJO.class, body);

            MyPOJO message = (MyPOJO) body;

            assertEquals("Some Field", someValue, message.getSomeField());

            // etc., etc.

        }           
    });


    //If I don't put some sleep here it ends before anything happens
    Thread.sleep(2000);

}

当我运行它时,它似乎运行正确,但是当我单步执行时,我可以看到断言失败。由于某种原因,这不会被报告。

所以然后我尝试像这样在路线中内联我的处理器:

public void configure() throws Exception {
    .from("file:///path/to/dir/of/test/file/?noop=true")
    .bean(TranslatorBean.class)
    .process(new Processor() {
        //same code as before
     });
}

这种工作,但有一个巨大的问题。JUnit 仍然不会报告任何失败的断言。相反,它们被 Camel 捕获,并报告为 CamelExecutionException,而完全没有关于原因的信息。只有通过调试器单步执行才能确定哪个断言失败。此外,这样我的整个测试都在 configure 方法中,而不是在它自己的测试方法中。我必须在睡眠中包含一个空测试才能使其运行。显然这很糟糕,但我确信在这里做正确的事情是。看起来处理器可能不是正确的路线,但我没有看到正确的方法。非常感谢任何指导。

4

2 回答 2

4

如果您正在寻找一种方法来检索对象本身并对其执行断言,您需要类似的东西:

Product resultProduct = resultEndpoint.getExchanges().get(0).getIn().getBody(Product.class);
assertEquals(expectedEANCode, resultProduct.getEANCode());

resultEndPoint模拟端点在哪里。

于 2015-02-04T14:44:10.203 回答
1

我建议从 Apache Camel 文档开始阅读。在用户指南:http ://camel.apache.org/user-guide.html有一个测试链接:http ://camel.apache.org/testing.html有很多信息。此外,由于您使用模拟端点,请阅读:http ://camel.apache.org/mock

例如,您拥有的模拟代码可以作为

MockEndpoint endpoint = getMockEndpoint("mock:test");
mock.allMessages().body().isInstanceOf(MyPojo.class)

尽管人们经常在模拟端点等上使用预期的方法,例如,如果您期望 2 条消息,并且它们的正文按以下顺序排列:

MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedBodiesReceived("Hello World", "Bye World");

但请查看模拟和测试文档以了解更多信息。

如果你有 Camel in Action 这本书的副本,那么请阅读第 6 章,它都是关于使用 Camel 进行测试的。

于 2013-04-07T07:25:24.473 回答