Versions Compared

Key

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

...

NameTesting Frameworks SupportedDescription
Camel CDI Test
  • JUnit 4

Available as of Camel 2.17

The Camel CDI test module ({{camel-test-cdi}}) comes with provides a JUnit that you can use to delegate all

Arquillian
  • JUnit 4
  • TestNG 5
Arquillian is a testing platform that handles all the plumbing of in-container testing with support for a wide range a target runtimecontainers.

Camel CDI Test

With this approach, your test classes use the JUnit test 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.

...

Here is a simple unit test using the CamelCdiRunner:

 

Code Block
languagejava
@RunWith(CamelCdiRunner.class)
public class CamelCdiRunnerTest {

    @Inject
    CamelContext context;

    @Test
    public void test() {
        assertThat("Camel context status is incorrect!",
            context.getStatus(),
            is(equalTo(ServiceStatus.Started)));
    }
}

...

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

    @Deployment
    public static Archive<?> createTestArchive() {
        return ShrinkWrap.create(WebArchive.class)
            .addClass(Application.class)
            .addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))
            .setWebXML(Paths.get("src/main/webapp/WEB-INF/web.xml").toFile());
    }

    @Test
    @RunAsClient
    public void test(@ArquillianResource URL url) throws Exception {
        assertThat(IOHelper.loadText(new URL(url, "camel/rest/hello").openStream()),
            is(equalTo("Hello World!\n")));
    }
}

 

Test patterns

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

Code Block
languagejava
void advice(@Observes CamelContextStartingEvent event,
            @Uri("mock:test") MockEndpoint messages,
            ModelCamelContext context) throws Exception {

    context.getRouteDefinition("route")
        .adviceWith(context, new AdviceWithRouteBuilder() {
            @Override
            public void configure() {
                weaveAddLast().to("mock:test");
            }
        });
}


See Also