如何在不更改生产代码的情况下测试骆驼路线? [英] How do I test a camel route without changing the production code?

查看:27
本文介绍了如何在不更改生产代码的情况下测试骆驼路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple camel route:

@Component
public class HelloRoute extends RouteBuilder {

    String startEndpoint;

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

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

For testing, everything I read says to add a mock endpoint that will store the results:

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

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

This means I have to change my code to include the mock, and it will run in production. The camel documentatiuon is pretty clear not to use mocks in production: https://camel.apache.org/mock.html

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

How do I write a unit test that is using the mock for evaluating the results, but at the same time the router class should run in production without any test code or other artificial and unnecessary endpoint, like

to("log:blah")

推荐答案

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

Here is what you can do in your test case

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

这会将mock:result"添加到路由的末尾.通过这种方式,您将能够修改用于测试的路由,而无需重写它们.

This will add "mock:result" to the end of the route. This way you will be able to modify routes for testing without rewriting them.

这篇关于如何在不更改生产代码的情况下测试骆驼路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆