A very common need in the lifecycle of a project is setting up integration testing. In this tutorial, we'll see how to set up this scenario using the Maven Cargo plugin.
2. Maven Integration Test Build Phases
Luckily, Maven has built-in support for this exact scenario, with the following phases of the default build lifecycle (from the Maven documentation):
- pre-integration-test: Perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
- integration-test: Process and deploy the package if necessary into an environment where integration tests can be run.
- post-integration-test: Perform actions required after integration tests have been executed. This may including cleaning up the environment.
3. Set Up Cargo Plugin
Let's go over the setup required, step by step.
3.1. Exclude Integration Tests from the Surefire Plugin
First, the maven-surefire-plugin is configured so that integration tests are excluded from the standard build lifecycle:
Exclusions are done via ant-style path expressions, so all integration tests must follow this pattern and end with “IntegrationTest.java“.
3.2. Configure the Cargo Plugin
Next, the cargo-maven3-plugin is used, as Cargo comes with top-notch out-of-the-box support for embedded web servers. Of course, if the server environment requires a specific configuration, cargo also knows how to construct the server out of an archived package as well as deploy to an external server.
A default embedded Jetty 9 web server is defined, listening on port 8080.
In the newer version of cargo (1.1.0 upwards), the default value of the wait flag has been changed to false, for cargo:start. This goal should only be used for running integration tests and is bound to the Maven lifecycle; for development, the cargo:run goal should be executed instead – which has wait=true.
In order for the package maven phase to generate a deployable war file, the packaging of the project must be <packaging>war</packaging>.
3.3. Add a New Maven Profile
Next, a new integration Maven profile is created to enable running the integration tests only when this profile is active, and not as part of the standard build lifecycle.
It is this profile that will contain all the remaining configuration details.
Now, the Jetty server is configured to start in the pre-integration-test phase and stop in the post-integration-test phase.
This ensures the cargo:start goal and cargo:stop goals will execute before and after the integration-test phase. Note that because there are two individual execution definitions, the id element must be present (and different) in both, so that Maven can accept the configuration.
3.4. Configure Integration Tests in the New Profile
Next, the maven-surefire-plugin configuration needs to be overridden inside the integration profile, so that the integration tests which were excluded in the default lifecycle are will now be included and run:
There are a few things worth noting:
1. The test goal of the maven-surefire-plugin is executed in the integration-test phase; at this point, Jetty is already started with the project deployed, so the integration tests should run with no problems.
2. The integration tests are now included in the execution. In order to achieve this, the exclusions are also overridden – this is because of the way Maven handles overriding plugin configurations inside profiles.
The base configuration is not completely overridden, but rather augmented with new configuration elements inside the profile.
Because of this, the original <excludes> configuration, which excluded the integration tests in the first place, is still present in the profile and needs to be overridden, or it would conflict with the <includes> configuration and the tests would still not run.
3. Note that, since there is only a single <execution> element, there is no need for an id to be defined.
Now, the entire process can run:
mvn clean install -Pintegration
The step-by-step configuration of Maven covers the entire process of setting up the integration process as part of the project lifecycle.
Usually, this is set up to run in a Continuous Integration environment, preferably after each commit. If the CI server already has a server running and consuming ports, then the cargo configuration will have to deal with that scenario, which I will cover in a future post.
For a fully running configuration of this mechanism, checkout out the REST GitHub project.
Also, check out this article for best practices of structuring a project and organizing the unit and integration tests.
res – Junit (guide) (cat=Testing)