ServiceMix (FuseESB) Unit Testing your routes
Today I will present simple way to test your camel routes. For building camel based service mix application, look into Part 4 of this tutorial series.
Let’s say we have the following route defined in a class extending RouteBuilder class:
from("activemq:test2").split(xpath("/notes/note")).parallelProcessing().setHeader("id", simple("123")).to("activemq:test3");
}
This route will pick up incoming messages on the activemq:test2 queue, split them and deliver to another queue (activemq:test3). To test the route we will replace activemq namespace within camel context with the camel component called ‘seda’ –
component provides asynchronous SEDA behavior, so that messages are exchanged on a BlockingQueue and consumers are invoked in a separate thread from the producer.
protected CamelContext createCamelContext() throws Exception {
CamelContext context = super.createCamelContext();
context.addComponent("activemq", context.getComponent("seda"));
return context;
}
to get the messages delivered to our end queue we are going to replace it with the mock queue, using an interceptor:
route.adviceWith(context, new RouteBuilder() {
public void configure() throws Exception {
interceptSendToEndpoint("activemq:test3")
.skipSendToOriginalEndpoint().to("mock:quote");
}
});
Full source of our unit test will look like this:
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;
public class MyRouteBuilderTest extends CamelTestSupport {
@Override
protected CamelContext createCamelContext() throws Exception {
CamelContext context = super.createCamelContext();
context.addComponent("activemq", context.getComponent("seda"));
return context;
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new MyRouteBuilder();
}
@Test
public void testMoveFile() throws Exception {
RouteDefinition route = context.getRouteDefinitions().get(0);
route.adviceWith(context, new RouteBuilder() {
public void configure() throws Exception {
interceptSendToEndpoint("activemq:test3")
.skipSendToOriginalEndpoint().to("mock:quote");
}
});
MockEndpoint mock = getMockEndpoint("mock:quote");
mock.expectedMessageCount(3);
mock.allMessages().body().isNotNull();
mock.allMessages().header("id").isEqualTo(123);
template.sendBody("activemq:test2", M_TXT);
assertMockEndpointsSatisfied();
}
private static final String M_TXT = "<?xml version="1.0" encoding="UTF-8"?>" +
"<notes>" +
"<note>" +
"<to>Anyone</to>" +
"<from>Kris1</from>" +
"<heading>Simple Message</heading>" +
"<body>Check out my Twitter account @grajo</body>" +
"</note>" +
"<note>" +
"<to>Anyone</to>" +
"<from>Kris2</from>" +
"<heading>Simple Message</heading>" +
"<body>Check out my Twitter account @grajo</body>" +
"</note>" +
"<note>" +
"<to>Anyone</to>" +
"<from>Kris3</from>" +
"<heading>Simple Message</heading>" +
"<body>Check out my Twitter account @grajo</body>" +
"</note>" +
"</notes>";
}
To use camel unit testing functionality you need to add the following dependencies to your project pom.xml file:
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
Hope that will help anyone to start testing your camel routes. More information and different use cases for mock component, interceptors as well as integration testing concepts can be found in the Camel In Action book