David
David

Reputation: 579

Is there a way to override a processor during unit testing?

I'm trying to write a unit test for one of my camel routes. In the route there is a processor that I would like to replace with a stub. Is there a way I can do this? I'm thinking of using the intercept feature but I can't seem to nail down the best way.

Example:

from(start)
    .process(myprocessor)
.to(end)

Thanks in advance.

Upvotes: 3

Views: 1985

Answers (3)

Gnana Guru
Gnana Guru

Reputation: 715

Yes you can do that by using Camel Advicedwith weaveById functionality which is used to replace the node during testing.

you have to set the id for your processor in the route and using that id you can weave whatever you want. Below is the example,

@Before
    protected void weaveMockPoints() throws Exception{

        context.getRouteDefinition("Route_ID").adviceWith(context,new AdviceWithRouteBuilder() {            
            @Override
            public void configure() throws Exception {              

                weaveById("myprocessorId").replace().to(someEndpoint);

            }
        });             

        context().start();
    }

Only thing is, you have to apply this to the route which is not started already. Better do the change whatever you want and then start your camelcontext as the above example.

Upvotes: 10

First you need extends CamelTestSupport: class MyTest extends CamelTestSupport {} after in your test method:

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

And in your route:

from(start)
.process(myprocessor).id("myprocessorId")
.to(end)

regards

Upvotes: 1

Alexey Yakunin
Alexey Yakunin

Reputation: 1771

IMHO, you need implementation of Detour EIP (http://camel.apache.org/detour.html).

from(start)
    .when().method("controlBean", "isDetour")
       .to("mock:detour")
    .endChoice()
    .otherwise()
       .process(myprocessor)
    .end()
.to(end)

Upvotes: 2

Related Questions