Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagexml
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-test-cdi</artifactId>
    <scope>test</test>scope>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

...

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

Code Block
languagejava
@RunWith(CamelCdiRunner.class)
public class CamelCdiTest {
 
    @Test
    @Order(1)
    public void firstTestMethod() {
    }
 
    @Test
    @Order(2)
    public void secondTestMethod() {
    }
}

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.:

...

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.

Info

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.:

Code Block
languagejava
@RunWith(CamelCdiRunner.class)
@Beans(classes = TestRoute.class)
public class CamelCdiTest {

    // ...
}

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.

...

Code Block
languagejava
@RunWith(Arquillian.class)
public class CamelCdiTest {

    @Deployment
    public static Archive deployment() {
        return ShrinkWrap.create(JavaArchive.class)
        // Camel CDI
        .addPackage(CdiCamelExtension.class.getPackage())
        // Test classes
        .addPackage(Application.class.getPackage())
        // Bean archive deployment descriptor
        .addAsManifestResource(
            new StringAsset(
                Descriptors.create(BeansDescriptor.class)
                    .getOrCreateAlternatives()
                        .stereotype(MockAlternative.class.getName()).up()
                    .exportAsString()),
                "beans.xml");
    }

    //...
}

Camel context

...

customization

You may need to customise 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.:

...