What is Cargo
Codehaus Cargo is really great maven plugin that allows you to manipulate with application container. Good point is that you can have a lot of types of application containers. For more information you can visit their web pages.
Usage for integration tests
In this post, we will configure a run of integration tests in build lifecycle. I found many articles about it but I’d like to create it more simply and mainly – descript parts in pom file. I have a rule: Use a comments! 🙂 When somebody else will read your code with some unknown features, comments will save his/her time!
Our usage description in few points:
- Exclude integration tests from build life cycle in surefire plugin;
- New maven profile for running tests with Cargo;
- Create dependency for Cargo with container definition and execution steps;
- Set phases for tests in Surefire plugin.
First step is exclude integration tests from build (in this example, we have a Test postix for test classes):
<!-- Exclude an integration test from lifecycle --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <excludes> <exclude>**/*Test.java</exclude> </excludes> </configuration> </plugin>
Now we will create in pom.xml new profile and implement Cargo and integration tests:
<profiles> <profile> <id>integration-tests</id> <build> <plugins> <!-- Configuration of container for running integration test and its running in lifecycle--> <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.4.8</version> <configuration> <!-- Container definition --> <container> <containerId>jetty7x</containerId> <type>embedded</type> </container> <configuration> <properties> <cargo.servlet.port>8080</cargo.servlet.port> </properties> </configuration> </configuration> <!-- Setting for container starting/stoping during phases. --> <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> <!-- Set test phase --> <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>**/*Test.java</include> </includes> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles>
Finally we are able to run our maven project with our created profile (-PprofileName) via command:
mvn clean install -Pintegration-test
First we excluded an integration tests from build. Then we created a new profile (called integration-tests) and defined a container as embedded Jetty and set phases for start and stop this container. Next we overloaded Surefire plugin for running integration tests (defined by Test postfix). There are a lot of ways how to configure Cargo plugin and you can look for help in their arcticles.