Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Warning
titleRecommendation

It is recommended to only advice a given route once (you can of course advice multiple routes). If you do it multiple times, then it may not work as expected, especially when any kind of error handling is involved.
The Camel team plan for Camel 3.0 to support this as internal refactorings in the routing engine is needed to support this properly.

Tip
titleTelling Camel you are using adviceWith

From Camel 2.9 onwards its recommended to override the isUseAdviceWith method and return true to tell Camel you are using advice with in your unit tests. Then after you have done the adviceWith, then you must start CamelContext manually. See further below for an example.

Using AdviceWithRouteBuilder

...

Method

Description

mockEndpoints

Is used to easily mock all endpoints. See more details and examples at Mock.

mockEndpoints(pattern)

Is used to easily mock endpoints using a pattern. See more details and examples at Mock. See below for pattern matching.

weaveById(pattern)

Is used to select node(s) matching by id's, and weave in the following nodes. See below for pattern matching and examples.

weaveByToString(pattern)

Is used to select nodes(s) matching by their toString representation, and weave in the following nodes. See below for pattern matching and examples.

weaveByType(Class)

Camel 2.8: Is used to select node(s) matching by their class type (the classes from the org.apache.camel.model package), and weave in the following nodes. See below for examples.

weaveAddFirst

Camel 2.8: Is a short hand to easily weave in the following nodes in the start of the route.

weaveAddLast

Camel 2.8: Is a short hand to easily weave in the following nodes in the end of the route.

replaceFromreplaceFromWith(uri)

Camel 2.9: To replace the route input to with a new endpoint uri.

The pattern option is used for matching. It uses the same rules as the Intercept, which is applied in the following order:

...

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

Replace from with another endpoint

Available as of Camel 2.9

...

Code Block
context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception {
        replaceFromreplaceFromWith("seda:foo");
    }
});

Using isUseAdviceWith

Available as of Camel 2.9
It is recommended to override the method isUseAdviceWith and return true to instruct Camel that you are using adviceWith in the unit tests. Then in your unit test methods, after you have done the adviceWith you must start CamelContext by invoke the start method on the context instance. In the following we have an example. The route is using ActiveMQ to route messages. What we would like to do in a unit test is to test the route, but without having to set and use ActiveMQ. We do not have ActiveMQ on the classpath. So for that we need to advice the route and replace ActiveMQ with for example a SEDA endpoint instead.

Wiki Markup
{snippet:id=e1|lang=java|title=isUseAdviceWith|url=camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/IsUseAdviceWithJUnit4Test.java}