gpt4 book ai didi

java - Camel 单元测试设置了对模拟组件的期望

转载 作者:行者123 更新时间:2023-12-01 11:55:23 25 4
gpt4 key购买 nike

我正在使用camel-test编写单元测试用例,遵循概述的步骤here 。在使用camel-test组件模拟现有端点部分下,有一个片段

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

我想做类似的事情,但我的主体类型是 POJO,没有重写的 equals 方法。

我试过了

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...]

消息的内容与我想要的完全一样,但是测试失败了。任何意见,将不胜感激。谢谢

最佳答案

试试这个,

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

关于java - Camel 单元测试设置了对模拟组件的期望,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28493234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com