骆驼单元测试对模拟组件设置期望

Camel unit tests setting expectations on mock component

我正在按照 here 概述的步骤使用 camel-test 编写单元测试用例。在 Mocking existing endpoints using the camel-test component 部分下,有一个片段

    getMockEndpoint("mock:direct:start").expectedBodiesReceived("Hello World");
    getMockEndpoint("mock:direct:foo").expectedBodiesReceived("Hello World");
    getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World");
    getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");

    template.sendBody("direct:start", "Hello World");

    assertMockEndpointsSatisfied();

我想做类似的事情,但我的 body 类型是一个没有覆盖 equals 方法的 POJO。

我试过了

getMockEndpoint("mock:result").message(0).method(new Object() {
    public boolean deepEquals(Exchange in) {
    MyPojo pojo = in.getIn().getBody(MyPojo.class); 

    return //custom pojo equals logic;
    }

}, "deepEquals").isEqualTo(true);

但我得到

Assertion error at index 0 on mock mock://result with predicate: BeanExpression[ method: deepEquals] == true evaluated as: null == true on Exchange[Message: MyPojo...]

邮件内容和我想要的一模一样,但是测试失败。任何意见,将不胜感激。谢谢

奇怪。我遇到了同样的错误。评估 bean 表达式的反射代码中似乎存在错误,可能是由于这是一个匿名内部 class.

试试这个:

getMockEndpoint("mock:result").message(0).method(new Foo(), "deepEquals").isEqualTo(true);

并将 deepEquals 方法移动到名为 class public static class Foo 的测试 class 某处。

试试这个,

final MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expects(new Runnable() {
        public void run() {

            MyPojo myPojo = mock.getExchanges().get(0).getIn().getBody(MyPojo.class);
            boolean status = //custom pojo equals logic;
            if(!status){
                fail("Testcase fails");
            }
        }
    });

另一种方式,

mock.whenAnyExchangeReceived(new Processor() {
        public void process(Exchange exchange) throws Exception {

            MyPojo myPojo = exchange.getIn().getBody(MyPojo.class);
            boolean status =//custom pojo equals logic;

            exchange.getIn().setBody(status);
        }
    });
    boolean out = template.requestBody(url, new MyPojo(), Boolean.class);
    assertEquals(true, out);