有没有一种方法可以在单元测试期间覆盖处理器



我正试图为我的一条骆驼路线编写一个单元测试。在路由中有一个处理器,我想用存根替换它。有什么办法我能做到这一点吗?我正在考虑使用拦截功能,但似乎无法确定最佳方式。

示例:

from(start)
    .process(myprocessor)
.to(end)

提前谢谢。

是的,您可以通过使用Camel AdvicedwithweaveById功能来做到这一点,该功能用于在测试期间替换节点。

你必须在路由中为你的处理器设置id,使用这个id你可以编织任何你想要的东西。以下是的示例

@Before
    protected void weaveMockPoints() throws Exception{
        context.getRouteDefinition("Route_ID").adviceWith(context,new AdviceWithRouteBuilder() {            
            @Override
            public void configure() throws Exception {              
                weaveById("myprocessorId").replace().to(someEndpoint);
            }
        });             
        context().start();
    }

唯一的问题是,你必须将其应用于尚未启动的路线。最好做任何你想做的改变,然后像上面的例子一样启动你的camelcontext。

IMHO,您需要实现Detour EIP(http://camel.apache.org/detour.html)。

from(start)
    .when().method("controlBean", "isDetour")
       .to("mock:detour")
    .endChoice()
    .otherwise()
       .process(myprocessor)
    .end()
.to(end)

首先需要扩展CamelTestSupport:类MyTest扩展了CamelTestSupport{}在你的测试方法之后:

context.getRouteDefinitions().get(0).adviceWith (context, new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception {
        weaveById("myprocessorId")
        .replace().to("mock:myprocessor");
    }
}

在你的路线上:

from(start)
.process(myprocessor).id("myprocessorId")
.to(end)

关于

最新更新