Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the asciidoc in the repository:

CDI Testing

Testing is a crucial part of any development or integration work. In case you're using the Camel CDI integration for your applications, you have a number of options to ease testing.

You can use CDI for IoC and the Camel testing endpoints like DataSetMockTest and testing API like AdviceWith and NotifyBuilder to create sophisticated integration/unit tests that are easy to run and debug inside your IDE.

There are a number of supported approaches for testing with CDI in Camel:

NameTesting Frameworks SupportedDescription
Camel CDI Test
  • JUnit 4

Available as of Camel 2.17

The Camel CDI test module (camel-test-cdi) provides a JUnit runner that bootstraps a test environment using CDI so that you don't have to be familiar with any CDI testing frameworks and can concentrate on the testing logic of your Camel CDI applications.

  • JUnit 4
  • TestNG 5
Arquillian is a testing platform that handles all the plumbing of in-container testing with support for a wide range of target containers. Arquillian can be configured to run your test classes in embedded (in JVM CDI), managed (a real Web server or Java EE application server instance started in a separate process) or remote (the lifecycle of the container isn't managed by Arquillian) modes. You have to create the System Under Test (SUT) in your test classes using ShrinkWrap descriptors. The benefit is that you have a very fine-grained control over the application configuration that you want to test. The downside is more code and more complex classpath / class loading structure.
PAX Exam
  • JUnit 4
  • TestNG 6
PAX Exam lets you test your Camel applications in OSGi, Java EE or standalone CDI containers with the ability to finely configure your System Under Test (SUT), similarly to Arquillian. You can use it to test your Camel CDI applications that target OSGi environments like Karaf with PAX CDI, but you can use it as well to test your Camel CDI applications in standalone CDI containers, Web containers and Java EE containers.

Camel CDI Test

With this approach, your test classes use the JUnit runner provided in Camel CDI test. This runner manages the lifecycle of a standalone CDI container and automatically assemble and deploy the System Under Test (SUT) based on the classpath into the container.

It deploys the test class as a CDI bean so that dependency injection and any CDI features is available within the test class.

Maven users will need to add the following dependency to their pom.xml for this component:

Here is a simple unit test using the CamelCdiRunner:

CDI injection is also available for test method parameters, e.g.:

Camel CDI test provides the @Order annotation that you can use to execute the test methods in a particular sequence, e.g.:

One CDI container is bootstrapped for the entire execution of the test class.

Besides, the test class is deployed as a CDI bean, so that you can control how the runner instantiate the test class, either one test class instance for each test method (the default, depending on the built-in default @Dependent CDI scope), or one test class instance for the entire test class execution using the @ApplicationScoped scope, e.g.:

In case you need to add additional test beans, you can use the @Beans annotation provided by Camel CDI test. For example, if you need to add a route to your Camel context, instead of declaring a RouteBuilder bean with a nested class, you can declare a managed bean, e.g.:

And add it with the @Beans annotation, e.g.:


With this approach, you use the JUnit runner or TestNG support provided by Arquillian to delegate the bootstrap of the CDI container. You need to declare a @Deployment method to create your application configuration to be deployed in the container using ShrinkWrap descriptors, e.g.:

In that example, you can use any Java SE Arquillian embedded container adapter, like the Weld embedded container adapter e.g. with Maven you need that complete set of dependencies:

Using ShrinkWarp Descriptors, you have a complete control over the configuration and kind of Camel CDI applications you want to test. For example, to test a Camel CDI application that uses the Camel REST DSL configured with the Servlet component, you need to create a Web archive, e.g.:

In the example above, you can use any Arquillian Web container adapter, like the Jetty embedded container adapter e.g. with Maven you need the complete following set of dependencies:

You can see the tests in the camel-example-cdi-rest-servlet example for a complete working example of testing a Camel CDI application using the REST DSL and deployed as a WAR in Jetty.

PAX Exam

If you target OSGi as runtime environment for your Camel CDI applications, you can use PAX Exam to automate the deployment of your tests into an OSGi container, for example into Karaf, e.g.:

You can see the tests in the camel-example-cdi-osgi example for a complete working example of testing a Camel CDI application deployed in an OSGi container using PAX Exam.

Testing Patterns

You can see the tests in the camel-example-cdi-test example for a thorough overview of the following testing patterns for Camel CDI applications.

While the patterns above are illustrated using the Camel CDI test module, they should equally work with Arquillian and PAX Exam unless otherwise stated or illustrated with a specific example.

Test routes

You may want to add some Camel routes to your Camel CDI applications for testing purpose. For example to route some exchanges to a MockEndpoint instance. You can do that by declaring a RouteBuilder bean within the test class as you would normally do in your application code, e.g.:

You can find more information in auto-detecting Camel routes.

In case you prefer declaring the RouteBuilder bean in a separate class, for example to share it more easily across multiple test classes, you can use the @Beans annotation to instruct Camel CDI test to deploy that class as a CDI bean, e.g.:

Bean alternatives

You may want to replace a bean that is used in your Camel routes by another bean for testing purpose, for example to mock it or change the behaviour of the application bean.

Imagine you have the following route in your application:

And the corresponding bean:

Then you can replace the bean above in your tests by declaring an alternative bean, annotated with @Alternative, e.g.:

And you need to activate (a.k.a. select in CDI terminology) this alternative bean in your tests. If your using the CamelCdiRunner JUnit runner, you can do that with the @Beans annotation provided by the Camel CDI test module, e.g.:

If you're using Arquillian as testing framework, you need to activate the alternative in your deployment method, e.g.: 

Camel context customization

You may need to customize your Camel contexts for testing purpose, for example disabling JMX management to avoid TCP port allocation conflict. You can do that by declaring a custom Camel context bean in your test class, e.g.:

In that example, the custom Camel context bean declared in the test class will be used during the test execution instead of the default Camel context bean provided by the Camel CDI component.

Routes advising with adviceWith

AdviceWith is used for testing Camel routes where you can advice an existing route before its being tested. It allows to add Intercept or weave routes for testing purpose, for example using the Mock component.

It is recommended to only advice routes which are not started already. To meet that requirement, you can use the CamelContextStartingEvent event by declaring an observer method in which you use adviceWith to add a mock endpoint at the end of your Camel route, e.g.:

JUnit rules

Camel CDI test starts the CDI container after all the JUnit class rules have executed.

That way, you can use JUnit class rules to initialise (resp. clean-up) resources that your test classes would require during their execution before the container initialises (resp. after the container has shutdown). For example, you could use an embedded JMS broker like ActiveMQ Artemis to test your Camel JMS application, e.g.:

Another use case is to assert the behaviour of your application after it has shutdown. In that case, you can use the Verifier rule, e.g.:

See Also

© 2004-2015 The Apache Software Foundation.
Apache Camel, Camel, Apache, the Apache feather logo, and the Apache Camel project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
Graphic Design By Hiram