Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
camel sample Junit test - how to replace a pollEnrich call to a 3rd party service in an existing Route with a constant
package biz.daich.learning.camel;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
import lombok.extern.slf4j.Slf4j;
public class Test_Eli_s3 extends CamelTestSupport {
protected RouteBuilder createRouteBuilder() throws Exception {
return new TestRoute();
private static class TestRoute extends RouteBuilder {
public void configure() throws Exception {
.process(e -> {"e={}", e.getIn().getBody(String.class));
public boolean isUseAdviceWith() {
return true;
public void test_1() throws Exception {
// get reference to the right route
RouteDefinition route = context.getRouteDefinition("a1");
// now intercept and replace the endpoints and nodes that are not relevant
// notice the class is AdviceWithRouteBuilder !!!
route.adviceWith(context, new AdviceWithRouteBuilder() {
public void configure() throws Exception {
weaveById("enrichID").replace().transform().constant("crap"); // replace the enrich node with the constant "crap" thats it!!!!
weaveById("httpServiceID").replace().transform().body(x -> x); /// replace the HTTP service call with just NO OP transform
weaveById("last").after().to("mock:end"); // add to the existing route a MOCK node at the end to do asserts that
// replaceFromWith("");
MockEndpoint mockEnd = getMockEndpoint("mock:end");
// now set the expectations::
// there was a message
/// another way to check that the right contents of the body of the messages arrived
mockEnd.expectedBodiesReceived(new Object[] {
// run the context
// send a message down the route
template.sendBody("direct:a", "123");
// check that all expectations on the mocks are as expected
/// this is the way to see what the mock Endpoint has AFTER running things through the routes
assertEquals("crap", mockEnd.getReceivedExchanges().get(0).getIn().getBody(String.class));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.