Camel Unit Testing – Using mock endpoint

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Hope you enjoyed the geo-coder example, am using the sample to explain some camel unit testing features.

I have already written an article on camel unit testing, this one would be more of a practical approach.

If you had a look at the routes, the first step is to remove the soap envelope as we are using cxf dataformat as MESSAGE.

Lets write a unit test to assert the soap envelope unwrapper. We are using xpath expression to remove the soap envelope from the incoming message body, this unit test helps to verify the xpath expression.

Camel route can be seen here: Geocoder Camel Route

Approach 1: Using mock endpoint.

1. Using camel adviseWith capabilities, add a mock endpoint after the soap-unwrapper xpath expression.

weaveById("soap-unwrapper").after().convertBodyTo(String.class).to("mock:intercept-body");

2. Send request payload to the from endpoint (Here we have mocked the cxf from endpoint with a direct, not required to load a cxf endpoint for this kind of unit testing)

template.sendBody("direct:geo-coder-gateway", requestPayload);

3. Extract the body of the message from the mock endpoint and assert

String response = getMockEndpoint("mock:intercept-body").getExchanges().get(0).getIn().getBody().toString();

Complete test case:

@Test
public void soapUnwrapperTest_usingMock() throws Exception {

context.getRouteDefinition("geocoder-service-callout")
.adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
replaceFromWith("direct:geo-coder-gateway");
weaveById("soap-unwrapper").after().convertBodyTo(String.class).to("mock:intercept-body");
}
});
context.start();

String requestPayload = IOUtils.toString(getClass().getResourceAsStream("/samples/service-request.xml"));

template.sendBody("direct:geo-coder-gateway", requestPayload);

String response = getMockEndpoint("mock:intercept-body").getExchanges().get(0).getIn().getBody().toString();

log.info("Response :"+response);

assertTrue(!response.contains("<soapenv:Envelope"));

}

 

Approach 2: Without using mock endpoint.

1. Using camel adviseWith capabilities, remove all the subsequent camel endpoints, processors, expressions.

weaveById("service-response-generator").remove();
weaveById("soap-wrapper").remove();
weaveById("reset-body").remove();

2. Instead of sendBody, use requestBody so that you can expect the response straight away.

String response = template.requestBody("direct:geo-coder-gateway", requestPayload).toString();

3. Assert the response.

Complete test case:

@Test
public void soapUnwrapperTest_withoutMock() throws Exception {

context.getRouteDefinition("geocoder-service-callout")
.adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
replaceFromWith("direct:geo-coder-gateway");
weaveById("service-response-generator").remove();
weaveById("soap-wrapper").remove();
weaveById("reset-body").remove();
}
});
context.start();

String requestPayload = IOUtils.toString(getClass().getResourceAsStream("/samples/service-request.xml"));

String response = template.requestBody("direct:geo-coder-gateway", requestPayload).toString();

log.info("Response :"+response);

assertTrue(!response.contains("<soapenv:Envelope"));

}

 

Test class can be found here: https://github.com/gnanagurus/camel-guru-projects/blob/master/camel-cxf-geocoder-example/src/test/java/org/camel/bushorn/testing/geoCoderServiceTest.java

 

Note/Tips:

1. For a highly complex route, using mock endpoint is very useful. It will be hectic to remove all the processors using weaveById(“bla”).remove();
2. Camel allows you to set ‘id’ for all processors, endpoints, etc. This will be very helpful while using AdviseWith features.

Useful links:

1. http://camel.apache.org/testing.html
2. http://bushorn.com/unit-testing-apache-camel/
3.https://github.com/gnanagurus/camel-guru-projects/blob/master/camel-cxf-geocoder-example/

Facebooktwittergoogle_plusredditpinterestlinkedinmail
Gnanaguru Sattanathan

Gnanaguru Sattanathan

I am a Integration developer and a open source enthusiast. My areas of interest includes Enterprise messaging & SOA. Twitter: @tallguru

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>