简体   繁体   English

通过过滤对 Apache Camel 特定路由进行单元测试 (Model#setRouteFilter)

[英]Unit test Apache Camel specific routes by filtering (Model#setRouteFilter)

How to include only certain routes in my unit test.如何在我的单元测试中仅包含某些路由。 For example, how do I enable only my-translation-route .例如,如何仅启用my-translation-route

public class TestRoute extends RouteBuilder {
    @Override
    public void configure() {
        from("ftp://my-ftp-server:21/messages")
                .routeId("my-inbound-route")
                .to("direct:my-translation-route");

        from("direct:my-translation-route")
                .routeId("my-translation-route")
                .bean(MyBean.class)
                .to("direct:my-outbound-route");

        from ("direct:my-outbound-route")
                .routeId("my-translation-route")
                .to("http://my-http-server:8080/messages");
    }
}

I tried with Model#filterRoutes but this did not work.我尝试使用 Model#filterRoutes 但这不起作用。 All routes were loaded.所有路线都已加载。

class TestRouteTest extends CamelTestSupport {
    @Override
    protected RoutesBuilder createRouteBuilder() {
        return new TestRoute();
    }

    @Override
    public boolean isUseAdviceWith() {
        return true;
    }

    @Test
    void testIfItWorks() throws Exception {
        context.setRouteFilterPattern("my-translation-route", null);

        AdviceWith.adviceWith(context, "my-translation-route", a -> {
            a.mockEndpointsAndSkip("direct:my-outbound-route");
        });

        context.start();

        getMockEndpoint("mock:direct:my-outbound-route").expectedBodyReceived().expression(constant("Hahaha! 42"));

        template.sendBodyAndHeaders("direct:my-translation-route", "42", null);

        assertMockEndpointsSatisfied();
    }
}

I got it working with the override of CamelTestSupport#getRouteFilterIncludePattern , eg:我让它与CamelTestSupport#getRouteFilterIncludePattern的覆盖一起工作,例如:

@Override
public String getRouteFilterIncludePattern() {
    return "direct:my-translation-route";
}

But then this is set for all tests in this test class.但是,这是为这个测试类中的所有测试设置的。

Possible (stupid) solution : set a conditional auto startup for your routes, whose value depends on a (Camel or JVM) property that you can set with a particular value during the unit tests:可能的(愚蠢的)解决方案:为您的路由设置有条件的自动启动,其值取决于您可以在单元测试期间使用特定值设置的(Camel 或 JVM)属性:

public class TestRoute extends RouteBuilder {

    @PropertyInject(name="productionMode", defaultValue="true")
    private boolean productionMode;

    @Override
    public void configure() {
        from("ftp://my-ftp-server:21/messages")
               ...
                .autoStartUp(productionMode); // <=here
     }
       
}

There are various ways to override properties during your tests.在测试期间有多种方法可以覆盖属性。 See https://camel.apache.org/components/3.17.x/properties-component.htmlhttps://camel.apache.org/components/3.17.x/properties-component.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM