0

Is there any way to mocking part of camel route?

I build such a route:

from("a").b().signReq().send().validateAns().c().to("d");

but when i run tests, i don't want add signReq().send().validateAns() into route. Any suggestions?

Also, maybe there is a way to encapsulate such part of route into method? It will be great, because i have many routes and many same interaction parts. Best if it can be done without runtime choice/when switches, because i know all conditions in configure phase.

cynepnaxa
  • 1,024
  • 1
  • 14
  • 30
  • 2
    I don't know the return type of each of your methods you are trying to mock, but keep in mind that you can partially mock an object. Take a look at this: http://stackoverflow.com/questions/14970516/use-mockito-to-mock-some-methods-but-not-others – Luke SpringWalker Oct 22 '14 at 03:32
  • Thanks for suggestion! But there is a lot to mock in thas objects, i want to mock and/or encapsule entire part of route. – cynepnaxa Oct 22 '14 at 04:33
  • As a thumb rule, if its hard to test, its time to refactor. – Luke SpringWalker Oct 22 '14 at 12:25

1 Answers1

0

For testing existing routes you can use AdviceWith and advice a route before its being tested.

I propose using weaveById which is the most precise way to replace parts of a route.

For example in the following route

from("direct:start")
    .to("mock:foo")
    .to("mock:bar").id("bar")
    .to("mock:result");

After setting the id of "mock:bar" to "bar" then you can use in your test

 context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            // weave the node in the route which has id = bar
            // and replace it with the following route path
            weaveById("bar").replace().multicast().to("mock:a").to("mock:b");
        }
    });

In you example you can do something like:

from("a").b().to("direct:replace").id("replace").c().to("d");

from("direct:replace").signReq().send().validateAns();

And afterwards advice the route by using:

 weaveById("replace").remove();

Of course more ways exist to implement this functionality. For all the options and a full example go to http://camel.apache.org/advicewith.html

Tip: Give extra attention to the part of the code in the example that starts the context!

// we must manually start when we are done with all the advice with
        context.start();
ltsallas
  • 1,918
  • 1
  • 12
  • 25