test-infra/camel-test-infra-core/README.md
One of the infrastructure components provided by test infra is a JUnit 5 extension that injects a Camel context into the tests.
This extension is called camel-test-infra-core.
It is an internal interface, meant to be used only by Camel itself, for very
specific test scenarios.
When testing Camel or a Camel-based integration, you almost certainly need to use the CamelContext to configure the registry, add routes and execute other operations. The test infra comes with a module that provides a JUnit 5 extension that allows you to inject a Camel context into your tests.
Adding it to your test code is as simple as adding the following lines of code to your test class:
@RegisterExtension
protected static CamelContextExtension contextExtension = new DefaultCamelContextExtension();
Then, via the extension, you can access the context (i.e.,: contextExtension.getContext()) to manipulate it as needed in the tests.
The extension comes with a few utilities to simplify configuring the context, and adding routes at the appropriate time.
To create a method that configures the context,
you can declare a method receiving an instance of CamelContext and annotate it with @ContextFixture.
@ContextFixture
public void configureContext(CamelContext context) {
// context configuration code here
}
Additionally, you can simplify the class hierarchy,
and ensure consistency you may also implement the ConfigurableContext interface.
You can configure the routes using a similar process as the one described for configuring the Camel context. You can create a method that receives an instance of CamelContext and annotate it with @RouteFixture.
@RouteFixture
public void createRouteBuilder(CamelContext context) throws Exception {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from(fromUri).to(destUri);
}
});
}
To start using the Camel Context extension on your code, add the following dependency:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-infra-core</artifactId>
<version>${camel.version}</version>
<scope>test</scope>
<type>test-jar</type>
</dependency>
For simplicity and consistency, you may also declare the route as implementing the ConfigurableRoute.
This section describes how to convert projects that create and manage a CamelContext directly (i.e.; not relying on CamelTestSupport).
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-infra-core</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
@RegisterExtension
private static CamelContextExtension camelContextExtension = new DefaultCamelContextExtension();
Tips: when running multiple tests in the same class, it may be necessary to completed trash the context instance. In this case, create an instance of TransientCamelContextExtension and JUnit will properly dispose the instance and create a new one.
private CamelContext context;
@BeforeEach
void setupTest() throws Exception {
context = camelContextExtension.getContext();
}
@RouteFixture annotation:@RouteFixture
public void setupRoute(CamelContext camelContext) throws Exception {
camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() {
restConfiguration()
.host("localhost")
.component("dummy-rest");
from("direct:foo")
.routeId("foo")
.to("mock:foo");
}
});
}
public method annotated with the @ContextFixture annotation:@ContextFixture
public void setupContext(CamelContext camelContext) throws Exception{
// configure the context
}
CamelTestSupport<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-infra-core</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
CamelTestSupport with the implementation of support interfaces from CamelTestSupportHelper, ConfigurableRoute and ConfigurableContext. These brings several helper methods from CamelTestSupport and simulate the legacy behavior.public class MyTest implements ConfigurableRoute, CamelTestSupportHelper {
// ...
}
@RegisterExtension
public static CamelContextExtension camelContextExtension = new DefaultCamelContextExtension();
Tips: when running multiple tests in the same class, it may be necessary to completed trash the context instance. In this case, create an instance of TransientCamelContextExtension and JUnit will properly dispose the instance and create a new one.
@Order(1)
@RegisterExtension
public static MyLocalContainerService service = new MyLocalContainerService();
@Order(2)
@RegisterExtension
public static CamelContextExtension camelContextExtension = new DefaultCamelContextExtension();
createRouteBuilder, so just create a new method that calls that old method, but, make sure to annotated it with the @RouteFixture annotation:@Override
@RouteFixture
public void createRouteBuilder(CamelContext context) throws Exception {
final RouteBuilder routeBuilder = createRouteBuilder();
if (routeBuilder != null) {
context.addRoutes(routeBuilder);
}
}
public method annotated with the @ContextFixture annotation:@ContextFixture
public void setupContext(CamelContext camelContext) throws Exception {
// configure the context
}