Versions Compared

Key

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

...

For example in the route below we intercept sending a message to the mock:foo endpoint and detour the message.

Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTest.java}

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.

Warning
titleRecommendation

It's recommended to only advice routes which are not started already.
If you advice already started routes, then it may not work as expected.

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.

...

Then let's go over the four methods to see how you can use them in unit tests:

Wiki Markup
{snippet:id=e1|lang=java|title=Replace|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTasksTest.java}
In this example we replace the .to("mock:bar").id("bar") with the .multicast().to("mock:a").to("mock:b").
That means instead of sending the message to a "mock:bar" endpoint, we do a Multicast to "mock:a" and "mock:b" endpoints instead.
Wiki Markup
{snippet:id=e2|lang=java|title=Remove|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTasksTest.java}
In the example above, we simply just remove the .to("mock:bar").id("bar").
Wiki Markup
{snippet:id=e3|lang=java|title=Before|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTasksTest.java}
In the example above, we add the following nodes to("mock:a").transform(constant("Bye World")) before the node with the id "bar".
That means the message being send to "mock:bar" would have been transformed to a constant message "Bye World".
Wiki Markup
{snippet:id=e4|lang=java|title=After|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTasksTest.java}
In the example above, we add the following nodes to("mock:a").transform(constant("Bye World")) after the node with the id "bar".

...

For example to replace any nodes which has "foo" you can do

Wiki Markup
{snippet:id=e1|lang=java|title=Replace|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTasksToStringPatternTest.java}
Notice that we have to use ".foo." in the pattern to match that "foo" is present anywhere in the string.

...

For example to remove a transform from the following route:

Wiki Markup
{snippet:id=e5|lang=java|title=Route|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTypeTest.java}
You can do:
Wiki Markup
{snippet:id=e2|lang=java|title=Remove|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTypeTest.java}

Using selectors

Available os of Camel 2.8

...

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/branches/camel-2.9.x/components/camel-test/src/test/java/org/apache/camel/test/IsUseAdviceWithJUnit4Test.java}