...
The camel-test
JAR is using JUnit. There is an alternative camel-testng
JAR (from Camel 2.8) using the TestNG test framework.
...
To get started using Camel Test you will need to add an entry to your pom.xml
:
JUnit
...
TestNG
Available as of Camel 2.8
...
...
You might also want to add slf4j
and log4j
to ensure nice logging messages (and maybe adding a log4j.properties file into your src/test/resources
directory).
...
Writing your test
You firstly need to derive from the class CamelTestSupport
(org.apache.camel.test.CamelTestSupport
, org.apache.camel.test.junit4.CamelTestSupport
, or org.apache.camel.testng.CamelTestSupport
for JUnit 3.x, JUnit 4.x, and TestNG, respectively) and typically you will need to override the createRouteBuilder()
or createRouteBuilders()
method to create routes to be tested.
Here is an example. Wiki Markup
...
Method Name | Description | Default Behavior |
---|---|---|
| If the route builders returned from either | Returns
|
| If the
Its important to start the | Returns The |
| See Setup CamelContext once per class, or per every test method. | The |
| Triggers the auto-mocking of endpoints whose URIs match the provided filter. The default filter is Return See | Disabled |
| If this method returns
are invoked for each processor in the registered routes. | Disabled The methods are not invoked during the test. |
| Returns the number of seconds that Camel should wait for graceful shutdown. Useful for decreasing test times when a message is still in flight at the end of the test. | 10 seconds |
| If JMX should be disabled on the | JMX is disabled |
| Provides a hook for adding objects into the registry. Override this method to bind objects to the registry before test methods are invoked. | An empty registry is initialized |
| Camel 2.10: Allows to add/override properties when Using PropertyPlaceholder in Camel. |
|
| Camel 2.10: Allows to control if Camel should ignore missing locations for properties. |
|
| Camel 2.16: If enabled, then Camel will dump all route coverage statistics into XML files in the target/camel-route-coverage directory. These XML files contains information about "route coverage" of all the routes that was used during the unit test. This allows tooling to inspect these XML files and generate nice route coverage reports. | Disabled |
...
Here is an example jndi.properties file
...
Dynamically Assigning Ports
...
Tests that use port numbers will fail if that port is already on use. AvailablePortFinder
provides methods for finding unused port numbers at run time.
...
...
Setup CamelContext once per class, or per every test method
...
The Camel Test kit will by default setup and shutdown CamelContext per every test method in your test class. So for example if you have 3 test methods, then CamelContext is started and shutdown after each test, that is 3 times.
...
...
This feature is also supported in camel-testng
...
When using this the CamelContext will keep state between tests, so have that in mind. So if your unit tests start to fail for no apparent reason, it could be due this fact. So use this feature with a bit of care.
You may want to do this once, to share the CamelContext between test methods, to speedup unit testing. This requires the use of JUnit 4! In your unit test method you have to extend the org.apache.camel.test.junit4.CamelTestSupport
or the org.apache.camel.test.junit4.CamelSpringTestSupport
test class and override the isCreateCamelContextPerClass
method and return true
as shown in the following example: Wiki Markup
See Also