【问题标题】:How do I test a camel route without changing the production code?如何在不更改生产代码的情况下测试骆驼路线?
【发布时间】:2017-04-03 10:22:15
【问题描述】:

我有一条简单的骆驼路线:

@Component
public class HelloRoute extends RouteBuilder {

    String startEndpoint;

    @Override
    public void configure() {
        from(startEndpoint).process(new HelloProcessor());
    }
}

对于测试,我读到的所有内容都说要添加一个模拟端点来存储结果:

from(startEndpoint).process(new HelloProcessor()).to("mock:result");

这意味着我必须更改我的代码以包含模拟,它将在生产中运行。骆驼文档很清楚不要在生产中使用模拟: https://camel.apache.org/mock.html

我如何编写一个单元测试,它使用模拟来评估结果,但同时路由器类应该在没有任何测试代码或其他人为和不必要的端点的情况下在生产中运行,例如

to("log:blah")

【问题讨论】:

标签: unit-testing apache-camel


【解决方案1】:

这是您可以在测试用例中执行的操作

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

这会将“mock:result”添加到路线的末尾。这样您就可以修改路由以进行测试,而无需重写它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 2018-09-05
    相关资源
    最近更新 更多