2022-06-30

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.

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. 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, e.g.:

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

But then this is set for all tests in this test class.



No comments:

Post a Comment