How to Test Camel applications with CamelTestSupport

In this tutorial we will learn how to Unit Test a Camel route using the CamelTestSupport Class. We will discuss first the advantages of using CamelTestSupport Class and then we will see a practical example of it.

Unit Testing with CamelTestSupport

The CamelTestSupport class is provided by Apache Camel as a base class for writing unit tests for Camel routes. It provides a set of utilities and features that simplify the process of testing Camel routes in an isolated and controlled environment. Here are some key purposes and features of the CamelTestSupport class:

  1. Setting up the Camel context: CamelTestSupport helps in setting up the Camel context required for testing. It creates an instance of the Camel context, initializes it, and provides access to the context in your test methods.
  2. Configuring routes: By overriding the createRouteBuilder() method, you can define and configure the routes that you want to test. The createRouteBuilder() method returns an instance of RouteBuilder, where you can define your routes using the Camel DSL.
  3. Testing route behavior: CamelTestSupport provides methods and assertions for testing the behavior of your Camel routes. For example, you can use getMockEndpoint to get a reference to a mock endpoint and set expectations on the number of messages received. You can also use template.sendBody to send test messages to your routes and assertMockEndpointsSatisfied to verify that the expected messages were processed.
  4. Preparing routes for testing: If you need to modify or mock certain aspects of your routes for testing purposes, CamelTestSupport allows you to use the AdviceWithRouteBuilder class. This class enables you to manipulate the route definition before it is started, such as replacing endpoints with mocks, adding interceptors, or modifying routing logic.
  5. Lifecycle management: CamelTestSupport handles the lifecycle management of the Camel context in your tests. It automatically starts and stops the context before and after each test method, ensuring a clean and consistent testing environment.
  6. Logging and debugging: CamelTestSupport provides built-in logging and debugging support for your tests. It configures the logging framework to capture and display log messages generated during the test execution. This can be helpful for troubleshooting and understanding the route behavior during testing.

A Sample Test Class

After the theory, we will show an example of a minimal Test Class which extends CamelTestSupport to Test a Camel Route:

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class CamelExampleTest extends CamelTestSupport {

  protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {
      public void configure() throws Exception {



  public void test() throws InterruptedException {
    System.out.println("running test");
    MockEndpoint resultEndpoint = context.getEndpoint("mock:result", MockEndpoint.class);
    context.createProducerTemplate().sendBody("direct:start", "Hello world");
    resultEndpoint.expectedBodiesReceived("Hello world");

You can run the above Test with:

mvn test

Finally, you should be able to verify that the Test passes, as our Assertion requires that the message sent to “direct:start” matches with the message in “mock:result”.

[                          main] CamelTestSupport               INFO  ********************************************************************************
[                          main] CamelTestSupport               INFO  Testing done: test(com.sample.CamelExampleTest)
[                          main] CamelTestSupport               INFO  Took: 11ms (11 millis)
[                          main] CamelTestSupport               INFO  ********************************************************************************


In conclusion, testing Camel routes using CamelTestSupport provides a powerful and efficient way to ensure the correctness and reliability of your Camel integration solutions. By leveraging the features and utilities provided by CamelTestSupport, developers can create comprehensive unit tests that cover different aspects of their routes’ behavior.

Found the article helpful? if so please follow us on Socials
Twitter Icon       Facebook Icon       LinkedIn Icon       Mastodon Icon