java - Camel单元测试命中自定义组件

标签 java routes apache-camel

我有写有组件(端点)的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();

}

引用:Camel AdviceWith documentation

关于java - Camel单元测试命中自定义组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13700892/

相关文章:

java - 如何确定是什么持有锁并阻塞了我的 Java 线程?

java - 纯 HTML 表单的 Spring 表单验证

ruby-on-rails - request.xhr CORS 布局失败

python - 使用 Flask 蓝图,如果指定了子域,如何修复 url_for 不被破坏?

java - Apache Camel 条件路由

xml - Camel xml 到 xml 转换 xslt 仍然是可行的方法吗?

java - Sysout 结果序列

java - kotlin:2个lambda参数的语法

c# - 带有 Swagger 文档的可选 Web API 路由参数

sockets - 接收套接字请求,通过各种端点,并使用 camel 中的 netty 响应同一个套接字连接