Apache Camel is an awesome framework, also providing a unit testing framework.
I found it quite tough to dive into that testing framework as testing a route is not the same than testing a regular Java class.
Here are some pattern when testing your Spring route :
More information on Camel test documentation
I found it quite tough to dive into that testing framework as testing a route is not the same than testing a regular Java class.
Here are some pattern when testing your Spring route :
Base code
The spring route
Assuming this route in camel.xml :Test class skeleton
And this JUnit test class, referencing- The XML route file
- The @DirtiesContext allowing us to manipulate the Camel Context on tests without effects outside of each test
@ContextConfiguration("classpath:camel.xml")
@DirtiesContext
public class MyTest extends AbstractJUnit4SpringContextTests{
@Autowired
private ModelCamelContext context;
@Test
public void testRouteId() throws Exception {
// introspect route
// create expectations
// execute route
// asserts
}
}
Patterns to test the route
Test a component
Here we are mocking the parameters provided to the WebService :- Create a MockEndpoint
- Insert endpoint in the route
- Add expectations for the endpoint :
- The number of messages
- The type of the message
- The content of the message
- At the end of the test method, trigger the assertion
@EndpointInject(uri = "mock:assertWsParams")
protected MockEndpoint assertWsParams;
...
//introspection
context.getRouteDefinition("route-id").adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveById("cxfWs").before().to(assertWsParams);
}
});
//expectations
assertWsParams.expectedMessageCount(1);
assertWsParams.expectedBodyReceived().body().isInstanceOf(RequestType.class);
assertWsParams.expectedMessagesMatches(new Predicate() {
@Override
public boolean matches(Exchange exchange) {
return ((RequestType) exchange.getIn().getBody()).getId().equals(EXPECTED_ID);
}
});
//execution
...
//assertion
assertWsParams.assertIsSatisfied();
Mock a component
Here we are mocking the cxf WebService with a static response :- Find the "node" with the weaveById
- replace() it
- with a process()
context.getRouteDefinition("route-id").adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveById("cxfWs").replace().process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
ReponseType res = new ResponseType();
res.setCode("200");
res.setMessage("OK");
exchange.getOut().setBody(res);
}
}).id("cxfWsMock");
}
});
Run the test
Here we are running the test, injecting some data in the route to test it and the assertions :- Create a producer on a uri
- Send some data
@Produce(uri = "direct:hello")
protected ProducerTemplate producer;
...
producer.sendBody("Hello");
Conclusion
My tests are mainly based on these 3 patterns, be sure to understand the logic.More information on Camel test documentation