Jennifer P.
Jennifer P.

Reputation: 387

How do I test a camel route without changing the production code?

I have a simple camel route:

@Component
public class HelloRoute extends RouteBuilder {

    String startEndpoint;

    @Override
    public void configure() {
        from(startEndpoint).process(new HelloProcessor());
    }
}

For testing, everything I read says to add a mock endpoint that will store the results:

from(startEndpoint).process(new HelloProcessor()).to("mock:result");

This means I have to change my code to include the mock, and it will run in production. The camel documentatiuon is pretty clear not to use mocks in production: https://camel.apache.org/mock.html

How do I write a unit test that is using the mock for evaluating the results, but at the same time the router class should run in production without any test code or other artificial and unnecessary endpoint, like

to("log:blah")

Upvotes: 4

Views: 1791

Answers (1)

Saurabh Nayar
Saurabh Nayar

Reputation: 407

Here is what you can do in your test case

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

This will add "mock:result" to the end of the route. This way you will be able to modify routes for testing without rewriting them.

Upvotes: 5

Related Questions