【问题标题】:Camel unit testing hits custom component骆驼单元测试命中自定义组件
【发布时间】:2012-12-04 10:32:02
【问题描述】:

我有编写组件(端点)的骆驼项目。它用于路由 (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 "*";
}

现在我所有的测试都很好。但是仍然不明白为什么会这样,我用直接替换端点并将消息发送到直接(不是真实的)

所以,非常抱歉,如果不是好问题,请关闭。

【问题讨论】:

    标签: java routing apache-camel


    【解决方案1】:

    当您在 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多