在骆驼中测试我的对象的正确方法

2022-01-19 00:00:00 java junit apache-camel

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

I'm trying to set up a test for a Camel route. My test route reads a binary file and sends it to a translator bean, returning a POJO. Now, I'd like to do some assertions on the POJO to ensure the values that are there match the known values. Standard stuff I think. In the examples I've seen, the body seems to always be a String or primitive type, and a simple assertion can be done on it. However, in my case, it is an object, so I want to get the object somehow.

这是我迄今为止尝试过的:

Here's what I've tried so far:

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

}

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

When I run this, it appears that it runs correctly, but when I step through, I can see an assertion fails. For some reason, this does not get reported.

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

So then I tried inlining my Processor in the route like so:

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 方法中,而不是在它自己的测试方法中.我必须在睡眠中包含一个空测试才能使其运行.显然这很糟糕,但我确信在这里做正确的事情是.看起来处理器可能不是正确的路线,但我没有看到正确的方法.非常感谢任何指导.

This kind of works, but with a huge problem. Any assertions that fail still do not get reported by JUnit. Instead, they get caught within Camel, and reported as a CamelExecutionException with absolutely no information about the cause. Only by stepping through the debugger can you determine which assertion failed. Also, this way my entire test is within the configure method instead of in its own test method. I have to include an empty test with a sleep to get it to run. Clearly this is terrible, but I'm sure what the right thing to do here is. It looks like a Processor might not be the correct route, but I'm not seeing the correct way. Any guidance is much appreciated.

推荐答案

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

If you're looking for a way to retrieve the object itself and perform assertions on it, you want something like:

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

其中 resultEndPoint 是模拟端点.

相关文章