[CONF] Apache Camel Spring Testing

2012-04-16 Thread confluence







Spring Testing
Page edited by David Valeri


 Changes (1)
 




...
| org.apache.camel.test.spring.LazyLoadTypeConverters (Deprecated) \\ | Class \\ | Indicates if thenbsp;CamelContexts that are bootstrapped during the test through the use of Spring Testnbsp;loaded application contexts should use lazy loading of type converters. | Type converters are not lazy loaded | Type converters are not lazy loaded | | org.apache.camel.test.spring.MockEndpoints \\ | Class \\ | Triggers the auto-mocking of endpoints whose URIs match the provided filter.nbsp; The defaultnbsp;filter is * which matches all endpoints. nbsp;Seenbsp;org.apache.camel.impl.InterceptSendToMockEndpointStrategynbsp;fornbsp;more details on the registration of the mock endpoints. | Not enabled | All endpoints are sniffed and recorded in a mock endpoint. | 
| org.apache.camel.test.spring.MockEndpointsAndSkip\\ | Class | Triggers the auto-mocking of endpoints whose URIs match the provided filter.nbsp; The defaultnbsp;filter is * which matches all endpoints. nbsp;Seenbsp;org.apache.camel.impl.InterceptSendToMockEndpointStrategynbsp;fornbsp;more details on the registration of the mock endpoints. nbsp;This annotation will also skip sending the message to matched endpoints as well.\\ | Not enabled\\ | All endpoints are sniffed and recorded in a mock endpoint. nbsp;The original endpoint is not invoked.\\ | 
| org.apache.camel.test.spring.ProvidesBreakpoint \\ | Method | Indicates that the annotated method returns annbsp;org.apache.camel.spi.Breakpointnbsp;for use in the test.nbsp; Useful for interceptingnbsp;traffic to all endpoints or simply for setting a break point in an IDE for debugging.nbsp; The method mustnbsp;be public, static, take no arguments, and return org.apache.camel.spi.Breakpoint. \\ | N/A | The returned Breakpoint is registered in the CamelContext(s) | | org.apache.camel.test.spring.ShutdownTimeout \\ | Class \\ | Indicates to set the shutdown timeout of all CamelContexts instantiated through thenbsp;use of Spring Test loaded application contexts.nbsp; If no annotation is used, the timeout isnbsp;automatically reduced to 10 seconds by the test framework. \\ | 10 seconds | 10 seconds | 
...


Full Content

Spring Testing

Testing is a crucial part of any development or integration work. The 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, and TestNG.

We can use Spring for IoC and the Camel Mock and Test endpoints to create sophisticated integration/unit tests that are easy to run and debug inside your IDE. There are three supported approaches for testing with Spring in Camel.



 Name 
 Testing Frameworks Supported 
 Description 
 Required Camel Test Dependencies 


 CamelSpringTestSupport 

	JUnit 3.x (deprecated)
	JUnit 4.x
	TestNG - Camel 2.8


 Provided by org.apache.camel.test.CamelSpringTestSupport, org.apache.camel.test.junit4.CamelSpringTestSupport, and org.apache.camel.testng.CamelSpringTestSupport. These base classes provide feature parity withthe simple CamelTestSupport classes fromCamel Testbut do not support Spring annotations on the test class such as @Autowired,@DirtiesContext, and@ContextConfiguration.  

	JUnit 3.x (deprecated) - camel-test-spring
	JUnit 4.x camel-test-spring
	TestNG - camel-test-ng




 Plain Spring Test  

	JUnit 3.x
	JUnit 4.x
	TestNG


 Extend the abstract base classes (org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests,org.springframework.test.context.junit38.AbstractJUnit4SpringContextTests, etc.)provided in Spring Test or use the Spring Test JUnit4 runner. These approaches support both the Camel annotations and Spring annotations, but do not have feature parity withorg.apache.camel.test.CamelTestSupport, org.apache.camel.test.junit4.CamelTestSupport, and org.apache.camel.testng.CamelSpringTestSupport.  

	JUnit 3.x (deprecated) - None
	JUnit 4.x None
	TestNG - None




 Camel Enhanced Spring Test  

	JUnit 4.x - Camel 2.10
	TestNG - Camel 2.10


 Use the org.apache.camel.test.junit4.CamelSpringJUnit4ClassRunnerrunner with the@RunWithannotation or extend org.apache.camel.testng.AbstractCamelTestNGSpringContextTeststo enable feature parity with org.apache.camel.test.CamelTestSupport and org.apache.camel.test.junit4.CamelTestSupport and also support the full suite of Spring Test annotations such as@Autowired,@DirtiesContext, and @ContextConfiguration. 

	JUnit 3.x (deprecated) - camel-test-spring
	JUnit 4.x camel-test-spring
	TestNG - camel-test-ng







CamelSpringTestSupport

org.apache.camel.test.CamelSpringTestSupport, org.apache.camel.test.junit4.CamelSpringTestSupport, and 

[CONF] Apache Camel Spring Testing

2012-03-02 Thread confluence







Spring Testing
Page edited by Babak Vahdat


 Changes (1)
 




...
h3. See Also  
* a A [real example test case using Mock and Spring|http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/test/TestEndpointTest.java?view=markup] Spring|https://svn.apache.org/repos/asf/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/mock/InterceptSendToMockEndpointStrategyTest.java] along with its [Spring XML|http://svn.apache.org/repos/asf/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/test/TestEndpointTest-context.xml] XML|https://svn.apache.org/repos/asf/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/InterceptSendToMockEndpointStrategyTest.xml] 
* [Bean Integration] * [Mock] endpoint 
...


Full Content

Spring Testing

Testing is a crucial part of any development or integration work. The 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, and TestNG.

We can use Spring for IoC and the Camel Mock and Test endpoints to create sophisticated integration/unit tests that are easy to run and debug inside your IDE. There are three supported approaches for testing with Spring in Camel.



 Name 
 Testing Frameworks Supported 
 Description 
 Required Camel Test Dependencies 


 CamelSpringTestSupport 

	JUnit 3.x (deprecated)
	JUnit 4.x
	TestNG - Camel 2.8


 Provided by org.apache.camel.test.CamelSpringTestSupport, org.apache.camel.test.junit4.CamelSpringTestSupport, and org.apache.camel.testng.CamelSpringTestSupport. These base classes provide feature parity withthe simple CamelTestSupport classes fromCamel Testbut do not support Spring annotations on the test class such as @Autowired,@DirtiesContext, and@ContextConfiguration.  

	JUnit 3.x (deprecated) - camel-test-spring
	JUnit 4.x camel-test-spring
	TestNG - camel-test-ng




 Plain Spring Test  

	JUnit 3.x
	JUnit 4.x
	TestNG


 Extend the abstract base classes (org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests,org.springframework.test.context.junit38.AbstractJUnit4SpringContextTests, etc.)provided in Spring Test or use the Spring Test JUnit4 runner. These approaches support both the Camel annotations and Spring annotations, but do not have feature parity withorg.apache.camel.test.CamelTestSupport, org.apache.camel.test.junit4.CamelTestSupport, and org.apache.camel.testng.CamelSpringTestSupport.  

	JUnit 3.x (deprecated) - None
	JUnit 4.x None
	TestNG - None




 Camel Enhanced Spring Test  

	JUnit 4.x - Camel 2.10
	TestNG - Camel 2.10


 Use the org.apache.camel.test.junit4.CamelSpringJUnit4ClassRunnerrunner with the@RunWithannotation or extend org.apache.camel.testng.AbstractCamelTestNGSpringContextTeststo enable feature parity with org.apache.camel.test.CamelTestSupport and org.apache.camel.test.junit4.CamelTestSupport and also support the full suite of Spring Test annotations such as@Autowired,@DirtiesContext, and @ContextConfiguration. 

	JUnit 3.x (deprecated) - camel-test-spring
	JUnit 4.x camel-test-spring
	TestNG - camel-test-ng







CamelSpringTestSupport

org.apache.camel.test.CamelSpringTestSupport, org.apache.camel.test.junit4.CamelSpringTestSupport, and org.apache.camel.testng.CamelSpringTestSupportextend their non-Spring aware counterparts (org.apache.camel.test.CamelTestSupport, org.apache.camel.test.junit4.CamelTestSupport, and org.apache.camel.testng.CamelTestSupport) and deliver integration with Spring into your test classes. Instead ofinstantiatingthe CamelContext and routes programmatically, these classes rely on a Spring context to wire the needed components together. If your test extends one of these classes, you must provide the Spring context by implementing the following method.



protected abstract AbstractApplicationContext createApplicationContext();


You are responsible for the instantiation of the Spring context in the method implementation. All of the features available in the non-Spring aware counterparts from Camel Test are available in your test.

Plain Spring Test

In this approach, your test classes directly inherit from the Spring Test abstract test classes or use the JUnit 4.x test runner provided in Spring Test. This approach supportsdependencyinjection into your test class and the full suite of Spring Test annotations but does not support the features provided by the CamelSpringTestSupport classes.

Plain Spring Test using JUnit 3.x with XML Config Example

Here is a simple unit test using JUnit 3.x support from Spring Test usingXML Config.

@ContextConfiguration
public class FilterTest extends 

[CONF] Apache Camel Spring Testing

2011-12-13 Thread confluence







Spring Testing
Page edited by Claus Ibsen


 Changes (1)
 




...
{code}  
You can overload the method {{createApplicationContext}} to provide the Spring ApplicationContext that isnt following the above default. For instance: {code:java}   protected AbstractXmlApplicationContext createApplicationContext() { return new ClassPathXmlApplicationContext(/config/MySpringConfig.xml);   } {code}  Then the test method will then run which invokes the [MockEndpoint.assertIsSatisfied(camelContext) method|http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/component/mock/MockEndpoint.html#assertIsSatisfied(org.apache.camel.CamelContext)] which asserts that all of the [Mock] and [Test] endpoints have their expectations met.  xml}  
h3. Spring Test with Java Config Example  
...


Full Content

Spring Testing

Testing is a crucial part of any development or integration work. The 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



Spring Test with Java Config Example

You can completely avoid using an XML configuration file by using Spring Java Config.

Here is an example using Java Config.


@ContextConfiguration(
locations = "org.apache.camel.spring.javaconfig.patterns.FilterTest$ContextConfig",
loader = JavaConfigContextLoader.class)
public class FilterTest extends AbstractJUnit4SpringContextTests {

@EndpointInject(uri = "mock:result")
protected MockEndpoint resultEndpoint;

@Produce(uri = "direct:start")
protected ProducerTemplate template;

@DirtiesContext
@Test
public void testSendMatchingMessage() throws Exception {
String expectedBody = "matched/";

resultEndpoint.expectedBodiesReceived(expectedBody);

template.sendBodyAndHeader(expectedBody, "foo", "bar");

resultEndpoint.assertIsSatisfied();
}

@DirtiesContext
@Test
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; so your entire test case is contained in a single Java class. We currently have to reference by class name this class in the @ContextConfiguration which is a bit ugly. Please vote for SJC-238 to address this and make Spring Test work more cleanly with Spring JavaConfig.

Adding more Mock expectations

If 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;