Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test multiple RouteBuilders in Apache Camel

I want to test multiple camel RouteBuilder in a single unit test what I have:

Custom camel processor that changes state

public class MyProcessor implements Processor {
   MyState state;
   public MyProcessor(MyState state) {this.state = state;}

   @Override
   public void process(Exchange exchange) throws Exception {
   state.setState(state.getState() + 5);
   }}

Two simple RouteBuilders: first routes messages from "direct:start" to "direct:endroute1" second pick up messages from "direct:endroute1" and routes somewhere "mock:endroute2"

public class MyRouteBuilder1 extends RouteBuilder {
    MyState state;
    public MyRouteBuilder1(MyState state) {this.state = state;}

    @Override
    public void configure() throws Exception {
        from("direct:start").process(new MyProcessor(state)).to("direct:endroute1");
    }}

public class MyRouteBuilder2 extends RouteBuilder {
    MyState state;
    public MyRouteBuilder2(MyState state) {this.state = state;}

    @Override
    public void configure() throws Exception {
        from("direct:endroute1").process(new MyProcessor(state)).to("mock:endroute2");
    }}

Writing unit test for a single route builder is straightforward:

public class MyTest extends CamelTestSupport {
    MyState state = new MyStateImpl();

    @EndpointInject(uri = "mock:result")
    protected MockEndpoint resultEndpoint;

    @Test
    public void testSingleRoute() throws Exception {
        resultEndpoint.expectedMessageCount(1);
        template.sendBody("direct:start", new Object());
        assertTrue(state.getState() == 5);
        resultEndpoint.assertIsSatisfied();
    }

    @Override
    protected RouteBuilder createRouteBuilder() {
     return new MyRouteBuilder1(state) {
            public void configure() throws Exception{
               super.configure();
               from("direct:endroute1").to("mock:result");
           }
     };
 }
}

What I really want to do is somehow to override CamelTestSupport.createRouteBuilder that will test whole chain of message processing from direct:start to mock:endroute2. As a result state.getState() should be 10

like image 290
Sergey Nesteruk Avatar asked Jan 22 '14 22:01

Sergey Nesteruk


2 Answers

Try to override method:

protected RouteBuilder[] createRouteBuilders() {...}

from CamelTestSupport. It's available since version 2.17

like image 86
ivict Avatar answered Oct 21 '22 04:10

ivict


you can just add multiple RouteBuilders to the context using the context.addRoutes(RouteBuilder) API

see this unit test for an example:

https://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/AddRoutesAtRuntimeTest.java

like image 30
Ben ODay Avatar answered Oct 21 '22 05:10

Ben ODay