|
Testing has been edited by James Strachan (Dec 03, 2008). Content:TestingTesting is a crucial activity in any piece of software development or integration. Typically Camel Riders use various different technologies wired together in a variety of patterns with different _expression_ languages together with different forms of Bean Integration and Dependency Injection so its very easy for things to go wrong! Camel is a Java library so you can easily wire up tests in whatever unit testing framework you use (JUnit 3.x, 4.x or TestNG). However the Camel project has tried to make the testing of Camel as easy and powerful as possible so we have introduced the following features. Testing mechanismsThe following mechanisms are supported
In all approaches the test classes look pretty much the same in that they all reuse the Camel binding and injection annotations. Camel Test ExampleHere is the Camel Test public class FilterTest extends CamelTestSupport {
@EndpointInject(uri="mock:result")
protected MockEndpoint resultEndpoint;
@Produce(uri="direct:start")
protected ProducerTemplate template;
public void testSendMatchingMessage() throws Exception {
String expectedBody = "<matched/>";
resultEndpoint.expectedBodiesReceived(expectedBody);
template.sendBodyAndHeader(expectedBody, "foo", "bar");
resultEndpoint.assertIsSatisfied();
}
public void testSendNotMatchingMessage() throws Exception {
resultEndpoint.expectedMessageCount(0);
template.sendBodyAndHeader("<notMatched/>", "foo", "notMatchedHeaderValue");
resultEndpoint.assertIsSatisfied();
}
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");
}
};
}
}
Notice how it derives from the Camel helper class CamelTestSupport but has no Spring or Guice dependency injection configuration but instead overrides the createRouteBuilder() method. Spring Test with XML Config ExampleHere is the Spring Test @ContextConfiguration public class FilterTest extends AbstractJUnit38SpringContextTests { @EndpointInject(uri = "mock:result") protected MockEndpoint resultEndpoint; @Produce(uri = "direct:start") protected ProducerTemplate template; @DirtiesContext public void testSendMatchingMessage() throws Exception { String expectedBody = "<matched/>"; resultEndpoint.expectedBodiesReceived(expectedBody); template.sendBodyAndHeader(expectedBody, "foo", "bar"); resultEndpoint.assertIsSatisfied(); } @DirtiesContext public void testSendNotMatchingMessage() throws Exception { resultEndpoint.expectedMessageCount(0); template.sendBodyAndHeader("<notMatched/>", "foo", "notMatchedHeaderValue"); resultEndpoint.assertIsSatisfied(); } } Notice the use of @ContextConfiguration to indicate that by default we should look for the FilterTest-context.xml on the classpath <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd "> <camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> <!-- Java version from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result"); --> <route> <from uri="direct:start"/> <filter> <xpath>$foo = 'bar'</xpath> <to uri="mock:result"/> </filter> </route> </camelContext> </beans> Spring Test with Java Config ExampleHere is the Spring Test @ContextConfiguration(
locations = "org.apache.camel.spring.javaconfig.patterns.FilterTest$ContextConfig",
loader = JavaConfigContextLoader.class)
public class FilterTest extends AbstractJUnit38SpringContextTests {
@EndpointInject(uri = "mock:result")
protected MockEndpoint resultEndpoint;
@Produce(uri = "direct:start")
protected ProducerTemplate template;
@DirtiesContext
public void testSendMatchingMessage() throws Exception {
String expectedBody = "<matched/>";
resultEndpoint.expectedBodiesReceived(expectedBody);
template.sendBodyAndHeader(expectedBody, "foo", "bar");
resultEndpoint.assertIsSatisfied();
}
@DirtiesContext
public void testSendNotMatchingMessage() throws Exception {
resultEndpoint.expectedMessageCount(0);
template.sendBodyAndHeader("<notMatched/>", "foo", "notMatchedHeaderValue");
resultEndpoint.assertIsSatisfied();
}
@Configuration
public static class ContextConfig extends SingleRouteCamelConfiguration {
@Bean
public RouteBuilder route() {
return new RouteBuilder() {
public void configure() {
from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");
}
};
}
}
}
This is similar to the XML Config example above except that there is no XML file and instead the nested ContextConfig class does all of the configuration. Testing endpointsCamel provides a number of endpoints which can make testing easier.
The main endpoint is the Mock endpoint which allows expectations to be added to different endpoints; you can then run your tests and assert that your expectations are met at the end. Stubbing out physical transport technologiesIf you wish to test out a route but want to avoid actually using a real physical transport (for example to unit test a transformation route rather than performing a full integration test) then the following endpoints can be useful.
|
Unsubscribe or edit your notifications preferences
