|
Mock has been edited by Claus Ibsen (Nov 09, 2008). Change summary: CAMEL-656 Mock ComponentTesting of distributed and asynchronous processing is notoriously difficult. The Mock, Test and DataSet endpoints work great with the Spring Testing framework to simplify your unit and integration testing using Enterprise Integration Patterns and Camel's large range of Components together with the powerful Mock and Test testing endpoints. The Mock component provides a powerful declarative testing mechanism which is similar to jMock This allows you to test various things like:
Note that there is also the Test endpoint which is-a Mock endpoint but which also uses a second endpoint to provide the list of expected message bodies and automatically sets up the Mock endpoint assertions. i.e. its a Mock endpoint which automatically sets up its assertions from some sample messages in a File or database for example. URI formatmock:someName?options Where someName can be any string to uniquely identify the endpoint
Simple ExampleHere's a simple example of MockEndpoint in use. First the endpoint is resolved on the context. Then we set an expectation, then after the test has run we assert our expectations are met. MockEndpoint resultEndpoint = context.resolveEndpoint("mock:foo", MockEndpoint.class); resultEndpoint.expectedMessageCount(2); // send some messages ... // now lets assert that the mock:foo endpoint received 2 messages resultEndpoint.assertIsSatisfied(); You typically always call the assertIsSatisfied() method Camel will by default wait 20 seconds when the assertIsSatisfied() is invoked. This can be configured by setting the setResultWaitTime(millis) method. Setting expectationsYou can see from the javadoc of MockEndpoint
Here's another example: resultEndpoint.expectedBodiesReceived("firstMessageBody", "secondMessageBody", "thirdMessageBody"); Adding expectations to specific messagesIn addition you can use the message(int messageIndex) For example to add expectations of the headers or body of the first message (using zero based indexing like java.util.List), you can use this code resultEndpoint.message(0).header("foo").isEqualTo("bar"); There are some examples of the Mock endpoint in use in the camel-core processor tests A Spring ExampleFirst here's the spring.xml file <camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="file:src/test/data?noop=true"/> <filter> <xpath>/person/city = 'London'</xpath> <to uri="mock:matched"/> </filter> </route> </camelContext> <bean id="myBean" class="org.apache.camel.spring.mock.MyAssertions" scope="singleton"/> As you can see it defines a simple routing rule which consumes messages from the local src/test/data directory Also note we instantiate a bean called myBean, here is the source of the MyAssertions bean public class MyAssertions implements InitializingBean { @EndpointInject(uri = "mock:matched") private MockEndpoint matched; @EndpointInject(uri = "mock:notMatched") private MockEndpoint notMatched; public void afterPropertiesSet() throws Exception { // lets add some expectations matched.expectedMessageCount(1); notMatched.expectedMessageCount(0); } public void assertEndpointsValid() throws Exception { // now lets perform some assertions that the test worked as we expect Assert.assertNotNull("Should have a matched endpoint", matched); Assert.assertNotNull("Should have a notMatched endpoint", notMatched); MockEndpoint.assertIsSatisfied(matched, notMatched); } } The bean is injected with a bunch of Mock endpoints using the @EndpointInject annotation, it then sets a bunch of expectations on startup (using Spring's InitializingBean interface and afterPropertiesSet() method) before the CamelContext starts up. Then in our test case (which could be JUnit or TesNG) we lookup myBean in Spring (or have it injected into our test) and then invoke the assertEndpointsValid() method on it to verify that the mock endpoints have their assertions met. You could then inspect the message exchanges that were delivered to any of the endpoints using the getReceivedExchanges() Here is the actual JUnit test case we use See Also |
Unsubscribe or edit your notifications preferences
