Skip to content
Advertisement

Can anyone tell me how to test my Camel Route if I have a choice operation?

I have a Camel route that has implemented a Content based Routing EIP(Choice operation). I need to test it. I’m new to Camel. So, I’m unsure how to do it. Can anyone tell me how to test this operation. I have mentioned a sample code below that has to be tested.

public void configure() throws Exception 
{   
    onException(Exception.class).handled(true).bean(ErrorHandler.class).stop();

    from("{{input}}?concurrentConsumers=10")
    .routeId("Actions")
        .choice()
            .when().simple("${header.Action} == ${type:status1}")
                .bean(Class, "method1")
            .when().simple("${header.Action} == ${type:status2}")
                .bean(Class, "method2")
            .when().simple("${header.Action} == ${type:status3}")
                .bean(Class, "method3")
            .otherwise()
                .bean(Class, "method4")
        .end();       
}

Advertisement

Answer

You can simply “advice” your route and add mocks to each choice of your content-based router

public void testAdvised() throws Exception {
    // advice the first route using the inlined route builder
    context.getRouteDefinition("Actions").adviceWith(context, new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            replaceFromWith("direct:start");
            weaveByToString(".*method1.*").after().to("mock:choice1");
            weaveByToString(".*method2.*").after().to("mock:choice2");
        }
    });

    getMockEndpoint("mock:choice1").expectedMessageCount(1);
    getMockEndpoint("mock:choice2").expectedMessageCount(0);

    template.sendBody("direct:start", "Hello World");

    assertMockEndpointsSatisfied();
}

This probably needs a little more modifications to get it working. Let me know if you needed more clarification or a proper real test scenario for your route.

Advertisement