|
Page Edited :
CAMEL :
Spring Testing
Spring Testing has been edited by Claus Ibsen (Sep 09, 2008). Content:Spring TestingThe Spring Framework offers a number of features that makes it easy to test while using Spring for Inversion of Control which works with JUnit 3.x, JUnit 4.x or TestNG We can reuse Spring for IoC and the Camel Mock and Test endpoints to create sophisticated integration tests that are easy to run and debug inside your IDE. For example here is a simple unit test import org.apache.camel.CamelContext; import org.apache.camel.component.mock.MockEndpoint; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests; @ContextConfiguration public class MyCamelTest extends AbstractJUnit38SpringContextTests { @Autowired protected CamelContext camelContext; public void testMocksAreValid() throws Exception { MockEndpoint.assertIsSatisfied(camelContext); } } This test will load a Spring XML configuration file called MyCamelTest-context.xml from the classpath in the same package structure as the MyCamelTest class and initialize it along with any Camel routes we define inside it, then inject the CamelContext instance into our test case. For instance, like this maven folder layout: src/main/java/com/mycompany/MyCamelTest.class src/main/resources/com/mycompany/MyCamelTest-context.xml You can overload the method createApplicationContext to provide the Spring ApplicationContext that isn't following the above default. For instance: protected AbstractXmlApplicationContext createApplicationContext() { return new ClassPathXmlApplicationContext("/config/MySpringConfig.xml"); } Then the test method will then run which invokes the MockEndpoint.assertIsSatisfied(camelContext) method Adding more Mock expectationsIf you wish to programmatically add any new assertions to your test you can easily do so with the following. Notice how we use @EndpointInject to inject a Camel endpoint into our code then the Mock API to add an expectation on a specific message. @ContextConfiguration public class MyCamelTest extends AbstractJUnit38SpringContextTests { @Autowired protected CamelContext camelContext; @EndpointInject(uri = "mock:foo") protected MockEndpoint foo; public void testMocksAreValid() throws Exception { // lets add more expectations foo.message(0).header("bar").isEqualTo("ABC"); MockEndpoint.assertIsSatisfied(camelContext); } } Further processing the received messagesSometimes once a Mock endpoint has received some messages you want to then process them further to add further assertions that your test case worked as you expect. So you can then process the received message exchanges if you like... @ContextConfiguration public class MyCamelTest extends AbstractJUnit38SpringContextTests { @Autowired protected CamelContext camelContext; @EndpointInject(uri = "mock:foo") protected MockEndpoint foo; public void testMocksAreValid() throws Exception { // lets add more expectations... MockEndpoint.assertIsSatisfied(camelContext); // now lets do some further assertions List<Exchange> list = foo.getReceivedExchanges(); for (Exchange exchange : list) { Message in = exchange.getIn(); ... } } } See Also
|
Unsubscribe or edit your notifications preferences
