我有写有组件(端点)的camel项目。它用于路由(mycomponent:somename) 组件完成后,我的路由单元测试开始失败。 (它使用一些与自己编写的客户端进行http通信)我可以根据“无法连接到服务器...”、“无法读取属性...”看到错误。
所以,看起来端点没有被我用于此目的的直接替换。 我需要的是,真正的路由单元测试,我不需要启动Web服务来运行一些测试,它应该只是路由测试。
示例代码:
public class MyTest extends CamelTestSupport {
@Override
public String isMockEndpoints() {
return "*";
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
includeRoutes(new MyRoute());
}
};
}
@Test
public void testRouteToRd() throws Exception {
context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
replaceFromWith("direct:incomponent"); // replacing from
}
});
getMockEndpoint("mock:myconnector:outcomponent").expectedMessageCount(1);
template.sendBody("direct:incomponent", OK_MESSAGE); // and sending to direct
assertMockEndpointsSatisfied();
}
}
为什么我开始使用此配置来命中真实组件(在组件完成之前它运行良好) 有人可以帮忙吗,谢谢。
我很抱歉,但是复制粘贴和“玩”配置对我有帮助。已替换
@Override
public String isMockEndpoints() {
return "*";
}
与:
@Override
public String isMockEndpointsAndSkip() {
return "*";
}
现在我所有的测试都很好。但仍然不明白为什么会发生这种情况,我将端点替换为直接并将消息发送到直接(不是真实的)
所以,非常抱歉,如果这不是一个好问题,请关闭。
最佳答案
当您在 Camel 测试中使用 adviceWith
时,您必须重写方法 isUseAdviceWith
,然后在调用之前调用 context.start()
发送您的数据。
示例:
@Override
public boolean isUseAdviceWith() {
// tell we are using advice with, which allows us to advice the route
// before Camel is being started
return true;
}
@Test
public void testRouteToRd() throws Exception {
context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
replaceFromWith("direct:incomponent"); // replacing from
}
});
context.start(); // NECESSARY IF USING ADVICEWITH
getMockEndpoint("mock:myconnector:outcomponent").expectedMessageCount(1);
template.sendBody("direct:incomponent", OK_MESSAGE); // and sending to direct
assertMockEndpointsSatisfied();
}
关于java - Camel单元测试命中自定义组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13700892/