Maven Cargo plugin for Integration Testing

A very common need in the lifecycle of a project is setting up integration testing.

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.

First, the maven-surefire-plugin is configured so that integration tests are excluded from the standard build lifecycle:

 <plugin>
    <groupId>org.apache.maven.plugins<groupId>
    <artifactId>maven-surefire-plugin<artifactId>
    <version>2.10<version>
    <configuration>
       <excludes>
          <exclude>***IntegrationTest.java<exclude>
       <excludes>
    <configuration>
 <plugin>

Exclusions are done via ant-style path expressions, so all integration tests must follow this pattern and end with “IntegrationTest.java“.

Next, the cargo-maven2-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 specific configuration, cargo also knows how to construct the server out of an archived package as well as deploy to an external server.

 <plugin>
    <groupId>org.codehaus.cargo<groupId>
    <artifactId>cargo-maven2-plugin<artifactId>
    <version>1.1.3<version>
    <configuration>
       <wait>true<wait>
       <container>
          <containerId>jetty7x<containerId>
          <type>embedded<type>
       <container>
       <configuration>
          <properties>
             <cargo.servlet.port>8080<cargo.servlet.port>
          <properties>
       <configuration>
    <configuration>
 <plugin>

An embedded Jetty 7 web server is defined, listening on port 8080. Notice the wait flag being set to true – this is because for the newer versions of cargo (1.1.0 upwards), the default value of the of the flag has changed from true to false, due to this bug. We want to be able to start the project by simply running mvn cargo:start, especially during the development phase, so the flag should be active. However, when running the integration tests we want the server to start, allow the tests to run and then stop, which is why the flag will be overridden later on.

In order for the package maven phase to generate a deployable war file, the packaging of the project must be: <packaging>war</packaging>.

Next, a new integration Maven profile is created to enable running the integration tests only when this profile is active, and not as part as the standard build lifecycle.

 <profiles>
    <profile>
       <id>integration<id>
       <build>
          
          <plugins>
             ...
          <plugins>
          
       <build>
    <profile>
 <profiles>

It is this profile that will contain all the remaining configuration.

Now, the Jetty server is configured to start in the pre-integration-test phase and stop in the post-integration-test phase.

 <plugin>
    <groupId>org.codehaus.cargo<groupId>
    <artifactId>cargo-maven2-plugin<artifactId>
    <configuration>
       <wait>false<wait>
    <configuration>
    <executions>
       <execution>
          <id>start-server<id>
          <phase>pre-integration-test<phase>
          <goals>
             <goal>start<goal>
          <goals>
       <execution>
       <execution>
          <id>stop-server<id>
          <phase>post-integration-test<phase>
          <goals>
             <goal>stop<goal>
          <goals>
       <execution>
    <executions>
 <plugin>

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 accepts the configuration.

Next, 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 included and run:

 <plugins>
    <plugin>
       <groupId>org.apache.maven.plugins<groupId>
       <artifactId>maven-surefire-plugin<artifactId>
       <executions>
          <execution>
             <phase>integration-test<phase>
             <goals>
                <goal>test<goal>
             <goals>
             <configuration>
                <excludes>
                   <exclude>none<exclude>
                <excludes>
                <includes>
                   <include>***IntegrationTest.java<include>
                <includes>
             <configuration>
          <execution>
       <executions>
    <plugin>
 <plugins>

There are a few things worth noting:

1. The test goal of the maven-surefire-plugin is executed in 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 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

Conclusion

The step by step configuration of Maven covers the entire process of setting up 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.

Reference: How to set up Integration Testing with the Maven Cargo plugin from our JCG partner Eugen Paraschiv at the baeldung blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


8 × = sixty four



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books
Get tutored by the Geeks! JCG Academy is a fact... Join Now
Hello. Add your message here.