Mock ComponentTesting of distributed and asynchronous processing is notoriously difficult. The Mock, Test and DataSet endpoints work great with the Camel Testing Framework to simplify your unit and integration testing using Enterprise Integration Patterns and Camel's large range of Components together with the powerful Bean Integration. The Mock component provides a powerful declarative testing mechanism, which is similar to jMock in that it allows declarative expectations to be created on any Mock endpoint before a test begins. Then the test is run, which typically fires messages to one or more endpoints, and finally the expectations can be asserted in a test case to ensure the system worked as expected. This allows you to test various things like:
Note that there is also the Test endpoint which is a Mock endpoint, but which uses a second endpoint to provide the list of expected message bodies and automatically sets up the Mock endpoint assertions. In other words, it's a Mock endpoint that automatically sets up its assertions from some sample messages in a File or database, for example. Mock endpoints keep received Exchanges in memory indefinitely Remember that Mock is designed for testing. When you add Mock endpoints to a route, each Exchange sent to the endpoint will be stored (to allow for later validation) in memory until explicitly reset or the JVM is restarted. If you are sending high volume and/or large messages, this may cause excessive memory use. If your goal is to test deployable routes inline, consider using NotifyBuilder or AdviceWith in your tests instead of adding Mock endpoints to routes directly. From Camel 2.10 onwards there are two new options URI formatWhere someName can be any string that uniquely identifies the endpoint. You can append query options to the URI in the following format, Options
Simple ExampleHere's a simple example of Mock endpoint in use. First, the endpoint is resolved on the context. Then we set an expectation, and then, after the test has run, we assert that our expectations have been met. You typically always call the assertIsSatisfied() method to test that the expectations were met after running a test. Camel will by default wait 10 seconds when the Using assertPeriodAvailable as of Camel 2.7 Setting expectationsYou can see from the javadoc of MockEndpoint the various helper methods you can use to set expectations. The main methods are as follows:
Here's another example: Adding expectations to specific messagesIn addition, you can use the message(int messageIndex) method to add assertions about a specific message that is received. For example, to add expectations of the headers or body of the first message (using zero-based indexing like There are some examples of the Mock endpoint in use in the camel-core processor tests. Mocking existing endpointsAvailable as of Camel 2.7 Camel now allows you to automatically mock existing endpoints in your Camel routes. How it works Important: The endpoints are still in action. What happens differently is that a Mock endpoint is injected and receives the message first and then delegates the message to the target endpoint. You can view this as a kind of intercept and delegate or endpoint listener. Suppose you have the given route below: Route You can then use the adviceWith mocking all endpoints Notice that the mock endpoints is given the uri Mocked endpoints are without parameters Endpoints which are mocked will have their parameters stripped off. For example the endpoint "log:foo?showAll=true" will be mocked to the following endpoint "mock:log:foo". Notice the parameters have been removed. Its also possible to only mock certain endpoints using a pattern. For example to mock all adviceWith mocking only log endpoints using a pattern The pattern supported can be a wildcard or a regular expression. See more details about this at Intercept as its the same matching function used by Camel. Mind that mocking endpoints causes the messages to be copied when they arrive on the mock. Mocking existing endpoints using the |