有没有办法在单元测试期间覆盖处理器?

Is there a way to override a processor during unit testing?

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

示例:

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

提前致谢。

恕我直言,您需要实施 Detour EIP (http://camel.apache.org/detour.html)。

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

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

你必须在路线中为你的处理器设置 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。

首先你需要扩展 CamelTestSupport: class 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)

问候