Mocking existing endpoints - 6.3

Talend ESB Mediation Developer Guide

EnrichVersion
6.3
EnrichProdName
Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
task
Design and Development
EnrichPlatform
Talend ESB

Camel now allows you to automatically mock existing endpoints in your Camel routes.

Important

The endpoints are still in action, what happens is that a Camel Component: Mock endpoint is injected and receives the message first, and then it delegates the message to the target endpoint. You can view this as a kind of intercept and delegate or endpoint listener.

Suppose you have the given route below:

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
   return new RouteBuilder() {
      @Override
      public void configure() throws Exception {
         from("direct:start").to("direct:foo").to("log:foo").to(
            "mock:result");

         from("direct:foo").transform(constant("Bye World"));
      }
   };
}

You can then use the adviceWith feature in Camel to mock all the endpoints in a given route from your unit test, as shown below:

public void testAdvisedMockEndpoints() throws Exception {
   // advice the first route using the inlined AdviceWith Route designer
   // which has extended capabilities than the regular Route designer
   context.getRouteDefinitions().get(0)
      .adviceWith(context, new AdviceWithRouteBuilder() {
      @Override
      public void configure() throws Exception {
         // mock all endpoints
         mockEndpoints();
      }
   });

   getMockEndpoint("mock:direct:start").
      expectedBodiesReceived("Hello World");
   getMockEndpoint("mock:direct:foo").
      expectedBodiesReceived("Hello World");
   getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World");
   getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");

   template.sendBody("direct:start", "Hello World");

   assertMockEndpointsSatisfied();

   // additional test to ensure correct endpoints in registry
   assertNotNull(context.hasEndpoint("direct:start"));
   assertNotNull(context.hasEndpoint("direct:foo"));
   assertNotNull(context.hasEndpoint("log:foo"));
   assertNotNull(context.hasEndpoint("mock:result"));

   // all the endpoints were mocked
   assertNotNull(context.hasEndpoint("mock:direct:start"));
   assertNotNull(context.hasEndpoint("mock:direct:foo"));
   assertNotNull(context.hasEndpoint("mock:log:foo"));
}

Notice that the mock endpoints is given the uri mock:<endpoint>, for example mock:direct:foo. Camel logs at INFO level the endpoints being mocked:

INFO  Adviced endpoint [direct://foo] with mock endpoint [mock:direct:foo]

Important

Endpoints which are mocked will have their parameters stripped off. For example the endpoint "log:foo?showAll=true" will be mocked to the following endpoint "mock:log:foo". Notice the parameters have been removed.

It is also possible to mock only certain endpoints using a pattern. For example to mock all log endpoints you can do as shown:

public void testAdvisedMockEndpointsWithPattern() throws Exception {
   // advice the first route using the inlined AdviceWith Route designer
   // which has extended capabilities than the regular Route designer
   context.getRouteDefinitions().get(0)
      .adviceWith(context, new AdviceWithRouteBuilder() {
      @Override
      public void configure() throws Exception {
         // mock only log endpoints
         mockEndpoints("log*");
      }
   });

   // now we can refer to log:foo as a mock and set our expectations
   getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World");

   getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");

   // rest of code as previous example
   ...
}

The pattern supported can be a wildcard or a regular expression. See more details about this functionality on the Apache Camel website.

Important

Mind that mocking endpoints causes the messages to be copied when they arrive on the mock. That means Camel will use more memory. This may not be suitable when you send in a lot of messages.