Mock ComponentTesting of distributed and asynchronous processing is notoriously difficult. The Mock Component provides a great tool for creating good integration test cases based on the Enterprise Integration Patterns using the various Components in Camel. The Mock component provides a powerful declarative testing mechanism which is similar to jMock This allows you to test various things like
URI formatmock:someName Where someName can be any string to uniquely identify the endpoint ExamplesHere'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 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 See Also |