...
Name | Testing Frameworks Supported | Description |
---|---|---|
Camel CDI Test |
| Available as of Camel 2.17 The Camel CDI test module ( |
Arquillian |
| Arquillian is a testing platform that handles all the plumbing of in-container testing with support for a wide range a target containers. Arquillian can be configured to run your test classes in embedded (in JVM CDI), managed (a real Web server or Java EE application server instance started in a separate process) or remote (the lifecycle of the container isn't managed by Arquillian) modes. You have to create the System Under Test (SUT) in your test classes using ShrinkWrap. The benefit is that you have a very fine-grained control over the application configuration that you want to test. The downside is more code and more complex classpath / class loading structure. |
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.
...
Code Block | ||
---|---|---|
| ||
@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)));
}
} |
CDI injection is also available for test method parameters, e.g.:
Code Block | ||
---|---|---|
| ||
@RunWith(CamelCdiRunner.class)
public class CamelCdiRunnerTest {
@Test
public void test(@Uri("direct:foo") ProducerTemplate producer) {
producer.sendBody("bar");
}
} |
Camel CDI test provides the @Order
annotation that you can use to execute the test methods in a particular sequence, e.g.:
Code Block | ||
---|---|---|
| ||
@RunWith(CamelCdiRunner.class)
public class CamelCdiRunnerTest {
@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.:
Code Block | ||
---|---|---|
| ||
@ApplicationScoped @RunWith(CamelCdiRunner.class) public class CamelCdiRunnerTest { int counter; @Test @Order(1) public void firstTestMethod() { counter++; } @Test @Order(2) public void secondTestMethod() { assertEquals(counter, 1); } } |
Arquillian
Code Block | ||
---|---|---|
| ||
@RunWith(Arquillian.class) public class CamelCdiJavaSeTest { @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 test() { assertThat("Camel context status is incorrect!", context.getStatus(), is(equalTo(ServiceStatus.Started))); } } |
Code Block | ||
---|---|---|
| ||
@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"))); } } |
...
Testing
Routes advising with adviceWith
...
Code Block | ||
---|---|---|
| ||
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"); } }); } |
JUnit rules
Camel CDI test starts the CDI container after all the JUnit class rules.
See Also
- CDI component
- Arquillian Web site
- CDI ecosystem
- Going further with CDI (See Camel CDI section)