在骆驼中测试我的物体的正确方法 [英] Right way to test my object in Camel

查看:92
本文介绍了在骆驼中测试我的物体的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为骆驼路线进行测试.我的测试路由读取一个二进制文件,并将其发送到翻译器bean,并返回POJO.现在,我想对POJO进行一些断言,以确保那里的值与已知值匹配.我认为是标准的东西.在我所看到的示例中,主体似乎始终是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是模拟端点.

这篇关于在骆驼中测试我的物体的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆