Versions Compared

Key

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

...

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.

 

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

Code Block
languagejava
@RunWith(CamelCdiRunner.class)
public class CamelCdiTest {
 
    // Declare a RouteBuilder bean for testing purpose
    // that is automatically added to the Camel context
    static class TestRoute extends RouteBuilder {

    @Override
    public void configure() {
        from("direct:out").routeId("test").to("mock:out");
    }
 
    // And retrieve the MockEndpoint for further assertions
    @Inject
    @Uri("mock:out")
    MockEndpoint mock;
}

...

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

...

Code Block
languagejava
@RunWith(CamelCdiRunner.class)
@Beans(alternatives = AlternativeBean.class)
public class CamelCdiTest {

    @Test
    public void testAlternativeBean(@Uri("direct:in") ProducerTemplate producer
                                    @Uri("mock:out") MockEndpoint mock) throws InterruptedException {
        mock.expectedMessageCount(1);
        mock.expectedBodiesReceived("test with alternative bean!");

        producer.sendBody("test");

        MockEndpoint.assertIsSatisfied(1L, TimeUnit.SECONDS, mock);
    }

    static class TestRoute extends RouteBuilder {

        @Override
        public void configure() {
            from("direct:out").routeId("test").to("mock:out");
        }
    }
}

...

Camel context customisation

...

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

Code Block
languagejava
@RunWith(CamelCdiRunner.class)
public class CamelCdiTest {
 
    @Default
    @ContextName("camel-test-cdi")
    @ApplicationScoped
    static class CustomCamelContext extends DefaultCamelContext {

        @PostConstruct
        void customize() {
            disableJMX();
        }
    }
}

...

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.

...