Test Spring JUnit5

Since Camel 3.0

The camel-test-spring-junit5 module is used for testing Camel with Spring; both the classic Spring XML files or Spring Boot.

Testing classic Spring XML

There are multiple approaches to test Camel Spring 5.x based routes with JUnit 5. An approach is to extend org.apache.camel.test.spring.junit5.CamelSpringTestSupport, for instance:

public class SimpleMockTest extends CamelSpringTestSupport {

    protected MockEndpoint resultEndpoint;

    protected ProducerTemplate template;

    protected AbstractApplicationContext createApplicationContext() {
        // loads a Spring XML file
        return new ClassPathXmlApplicationContext("org/apache/camel/test/patterns/SimpleMockTest.xml");

    public void testMock() throws Exception {
        String expectedBody = "Hello World";
        template.sendBodyAndHeader(expectedBody, "foo", "bar");

The example above is loading a classic Spring XML file (has <beans> as root tag).

This approach provides feature parity with org.apache.camel.test.junit5.CamelTestSupport from camel-test-junit5 but does not support Spring annotations on the test class such as @Autowired, @DirtiesContext, and @ContextConfiguration.

Instead of instantiating the CamelContext and routes programmatically, this class relies on a Spring context to wire the needed components together. If your test extends this class, you must provide the Spring context by implementing the following method:

protected abstract AbstractApplicationContext createApplicationContext();

Using the @CamelSpringTest annotation

A better and recommended approach involves the usage of the org.apache.camel.test.spring.junit5.CamelSpringTest annotation, as shown:

package com.foo;

@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class CamelSpringPlainTest {

    protected CamelContext camelContext;

    protected MockEndpoint mockA;

    protected MockEndpoint mockB;

    protected ProducerTemplate start;

    public void testPositive() throws Exception {
        assertEquals(ServiceStatus.Started, camelContext.getStatus());

        mockB.expectedBodiesReceived("Hello David");



The above test will by default load a Spring XML file using the naming pattern className-context.xml, which means the example above loads the file com/foo/CamelSpringPlainTest-context.xml.

This XML file is Spring XML file as shown:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
		http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">

	<camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
			<from uri="direct:start"/>
			<to uri="mock:a"/>
				<simple>Hello ${body}</simple>
			<to uri="mock:b"/>

This approach supports both Camel and Spring annotations, such as @Autowired, @DirtiesContext, and @ContextConfiguration. However, it does NOT have feature parity with org.apache.camel.test.junit5.CamelTestSupport.

Camel test annotations

The following annotations can be used with camel-spring-junit5 unit testing.

Annotation Description


Used for testing Camel with Spring Boot


Used for testing Camel with classic Spring XML (not Spring Boot)


Used for disabling JMX


Enables dumping route coverage statistic. The route coverage status is written as xml files in the target/camel-route-coverage directory after the test has finished. See more information at Camel Maven Report Plugin.


Indicates if certain route builder classes should be excluded from package scan discovery


Auto-mocking of endpoints whose URIs match the provided filter. For more information see Advice With.


Auto-mocking of endpoints whose URIs match the provided filter with the added provision that the endpoints are also skipped. For more information see Advice With.


Indicates that the annotated method returns a Breakpoint for use in the test. Useful for intercepting traffic to all endpoints or simply for setting a break point in an IDE for debugging. The method must be public static, take no arguments, and return Breakpoint.


Timeout to use for shutdown. The default is 10 seconds.


To enable testing with Advice With.


To use custom Properties with the Properties component. The annotated method must be public and return Properties.

Migrating Camel Spring Tests from JUnit 4 to JUnit 5

Find below some hints to help in migrating Camel Spring tests from JUnit 4 to JUnit 5.

Projects using camel-test-spring would need to use camel-test-spring-junit5. For instance, maven users would update their pom.xml file as below:

It’s possible to run JUnit 4 & JUnit 5 based Camel Spring tests side by side including the following dependencies camel-test-spring, camel-test-spring-junit5 and junit-vintage-engine. This configuration allows migrating Camel tests one by one.

Migration steps

  • Migration steps from camel-test-junit5 should have been applied first

  • Imports of org.apache.camel.test.spring.* should be replaced with org.apache.camel.test.spring.junit5.*

  • Usage of @RunWith(CamelSpringRunner.class) should be replaced with @CamelSpringTest

  • Usage of @BootstrapWith(CamelTestContextBootstrapper.class) should be replaced with @CamelSpringTest

  • Usage of @RunWith(CamelSpringBootRunner.class) should be replaced with @CamelSpringBootTest