Versions Compared

Key

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

...

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.

It deploys the test class as a CDI bean so that dependency injection and any CDI features is available within the test class.

...

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

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)));
    }
}



Arquillian


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

    @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(EmptyAsset.INSTANCE, "beans.xml");
    }
 
    @Inject
    CamelContext context;

    @Test
    public void testContexttest() {
        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")));
    }
}