Auto-mocking of endpoints

In the recipes that have involved mock testing so far, it has been necessary to provide a mock: endpoint URI directly into a route, either explicitly or through dependency injection. Camel's test support classes provide a mechanism for automatically mocking endpoints that allow you to more easily test routes with embedded URIs. This recipe will show you how to exercise this functionality.

Getting ready

The Java code for this recipe is located in the org.camelcookbook.examples.testing.automocking package. The Spring route used here is located under src/main/resources/META-INF/spring/fixedEndpoints-context.xml.

How to do it...

Consider the following route, with fixed endpoints:

<from uri="activemq:in"/>
<transform>
  <simple>Modified: ${body}</simple>
</transform>
<log message="Set message to ${body}"/>
<to uri="activemq:out"/>

In order to set up a mock endpoint on the to(..) node without changing the route, perform the following steps:

  1. Create a test that extends CamelSpringTestSupport as described in the Testing routes defined in Spring recipe, loading the preceding XML configuration. Then, override the isMockEndpoints() method, returning the name (endpoint URI) of the endpoint(s) that you would like to override:
    @Override
    public String isMockEndpoints() {
      return "activemq:out";
    }

    If using Enhanced Spring Testing, your test class should be annotated with:

    @MockEndpoints("activemq:out")
  2. A mock endpoint can now be fetched from the Camel context, through the mocked endpoint URI, prefixed with mock:
    @EndpointInject(uri="mock:activemq:out")
    MockEndpoint mockOut;

    It is now possible to test the route as usual, by sending in a message to the start endpoint, and asserting expectations on the MockEndpoint interface.

How it works...

Camel will create a mock endpoint for the URIs that you have specified, and use the full name of the original URI, including the scheme (activemq in the preceding example), as the name portion of the mock: endpoint URI.

As such, activemq:out becomes mock:activemq:out.

Auto-mocking does not replace the existing endpoints. A message will still be sent to activemq:out in the previous example, as well as mock:activemq:out. To skip sending to the original endpoint, you should override the isMockEndpointsAndSkip() method instead of isMockEndpoints().

The String describing which endpoints to mock may be a full endpoint URI, a wildcard (*), or a regular expression. It may also contain {{..}} property placeholders, which will be resolved first, before the remainder of the expression is processed.

One thing to note is that if the endpoint URI to be mocked contains attributes, then the matching String used in isMockEndpoints() or @MockEndpoints needs to use a wildcard in order for the framework to identify a match against the URI:

Considering the original endpoint URI:

activemq:out?timeToLive=10000

The following wildcard will match that as an endpoint to be mocked:

activemq:out?*

The URI that is then used to fetch the mock endpoint from the Camel context will not contain any of the original endpoint URI attributes

mock:activemq:out

There's more...

If you want to mock multiple endpoints within your route, you can specify the list as a regular expression:

(activemq:first|activemq:second)

You will then be able to access these as usual:

@EndpointInject(uri="mock:activemq:first")
@EndpointInject(uri="mock:activemq:second")

The approach of overriding isMockEndpoints() applies to not only to testing Spring-based routes, but those defined in OSGi Blueprint, and Java RouteBuilder implementations as well. The method is defined on CamelTestSupport, which both CamelSpringTestSupport and CamelBlueprintTestSupport extend.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.168.8