Maven Integration Test
Last Updated on Tuesday, 02 April 2013 08:01
Let's imagine you have some integration test and you want to put them in your Maven test folder. The Surefire Plugin is used during the test phase of the build lifecycle to execute your Java Unit tests.
Maven takes all the Java Test cases implementing JUnit library and execute them to define if the project can pass to the next lifecycle phase.
It is probably helpful to define what is a unit test and what is an integration test. Integration tests are mandatory to have reliable deliverable it is important to integrate them in our process.
It is an automatic test to test the internal workings of a class. It should be a stand-alone test which is not related to other resources.
Is an automatic test that is done on an environment, so similar to unit tests but with external resources (db, disk access)
Because the integration tests are dependent on external resources you don't want to rely on them to pass the test phase. Maven should be able to package your solution wherever you are.
To do so find below a way to achieve this objective :
Rename all your Java integration tests using the same naming convention :
To tell the test plugin of Maven (Surefire) to exclude them from the test lifecycle we will configure the plugin using the pattern :
**/*IntegrationTest.java to exclude the integration tests
By default the Failsafe Maven Plugin looks for integration tests matching the patterns */IT.java, **/IT.java and */*ITCase.java. You will notice that these bindings do not overlap with the default surefire bindings. To use the Maven Failsafe Plugin you need to add the following to your
The advantage to using the Maven Failsafe Plugin is that it will not stop the build during the integration-test phase if there are test failures. The recommendation is that you do not directly invoke the pre-integration-test, integration-test or post-integration-test phases but that instead you run integration tests by specifying the verify phase, e.g.
Congratulation ! You can now put your integration tests within your test folder and Maven is able to distinguish between unit tests and integration tests to run only the unit tests during the test phase
You are now able to run tests using continuous integration systems like Hudson or Continuum without having integration tests failing due to missing external resources.
Integration Tests with Junit 4.7
As of JUnit 4.7 you have you can define categories for your tests. It enables you to define groups or labels on classes or intefaces. I will demonstates how to use it with Maven surefire.
Three steps are required to prevent your integration tests to execute during your build cycle :
- Create a marker interface for your integration tests
- Mark you integration test classes using the @Category annotation
- Configure Maven surefice to exclude the corresponding categories in your unit test cycle
In the first step we need to create a marker interface to mark all of the tests you want to skip from the unit testing process
Mark your integration test class using Junit category annotation at the top of your class
Configure Maven Surefire plugin to include junit in version 4.7 and define the groups you want to exclude using the tag excludedGroups
Run the integration test during the maven integration-test phase
If you want to run your integration tests during the integration-test phase you can une the following Maven snippet
If you have any remark or questions feel free to put a comment.If you enjoyed this tutorial and want to promote it don't hesitate to click on
- Maven installation procedure for Windows
- Maven installation guide for Linux
- Maven release
- Maven compilation plugin tutorial
- Find unused jars using Maven
- Maven Dependency Tree
- Maven and Cobertura configuration
Tags: maven, build, integration, tests, test, plugin, unit
Sébastien Dante Ursini
17 Years of experience in Java
22 Year in Banking/Finance
Based in Geneva/Switzerland