...
You can see the tests in the camel-example-cdi-osgi
example for a complete working example of testing a Camel CDI application deployed in an OSGi container using PAX Exam.
Testing Patterns
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.
...
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"); } }); } |
Bean alternatives
Test routes
Camel context customisation
JUnit rules
Camel CDI test starts the CDI container after all the JUnit class rules have executed.
That way, you can use JUnit class rules to initialise (resp. clean-up) resources that your test classes would require during their execution before the container initialises (resp. after the container has shutdown). For example, you could use an embedded JMS broker like ActiveMQ Artemis to test your Camel JMS application, e.g.:
Code Block | ||
---|---|---|
| ||
import org.apache.activemq.artemis.jms.server.embedded.EmbeddedJMS;
@RunWith(CamelCdiRunner.class)
public class CamelCdiTest {
@ClassRule
public static final ExternalResource resources = new ExternalResource() {
private final EmbeddedJMS jms = new EmbeddedJMS();
@Override
protected void before() throws Exception {
jms.start();
}
@Override
protected void after() throws Exception {
jms.stop();
}
};
@Inject
@Uri("jms:destination")
private ProducerTemplate producer;
@Test
public void sendMessage() {
producer.sendBody("message");
}
} |
Another use case is to assert the behaviour of your application after it has shutdown. In that case, you can use the Verifier
rule, e.g.:
Code Block | ||
---|---|---|
| ||
@RunWith(CamelCdiRunner.class)
public class CamelCdiTest {
@ClassRule
public static Verifier verifier = new Verifier() {
@Override
protected void verify() {
// Executes after the CDI container has shutdown
}
};
} |