Over a million developers have joined DZone.

Separating Maven Unit & Integration Tests

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

In this example I will show how you can perform a standard Maven build whilst keeping your unit and integration tests in separate packages.

Other techniques specify using either a naming convention or a separate module to do this.
This example allows true package separation and is self contained in a single profile so it can easily be used across projects. Alternative profiles can also be created for different types of test.

This solution lends itself well to generating code coverage metrics using Sonar and Jacoco.

The first section shows what happens in the example and how to run it.
The second section explains how it works.

1.1 Example Structure

Here we have the typical maven structure and our new folder \src\integrationtest\java

1.2 Running the Example

The full code is hosted at google code. Use the following commands to check it out and run it.

svn co https://designbycontract.googlecode.com/svn/trunk/failsafe
cd failsafe
mvn –Pit clean integration-test

Alternatively you can look at the Hudson build here :https://designbycontract.ci.cloudbees.com/job/maven_with_split_tests/

1.3 Results of running the example

  • The tests in the standard maven test structure are run during the unit test phase as usual.
  • The tests in the \src\integrationtest\java directory are run during the integration test phase.
  • The integration test classes are placed in the seperate \target\integrationtest-classes directory

1.4 Isn't there an easier way?

Maven doesn’t really have a tidy way of dealing with this. Common suggestions are discussed here. http://olemortenamundsen.wordpress.com/2009/07/22/strategies-for-separating-unit-and-integration-tests-using-maven-eclipse-idea-cobertura/

The main ones are

  • Use \src\test package and use a separate directory or naming convention.
  • Put the integration tests in a separate module.

Maven does allow you to change the test source directories as demonstrated. The problem is that the Maven properties are immutable so you can't change them twice in the same build.

Firstly, we need to keep them at the defaults to enable our unit tests to run correctly.


Secondly we need to change them as below for our integration tests to be run correctly.


Unfortunately we cannot do this because the properties are immutable and cannot be both in the same build. However by using a number of different plugins and settings we can get the above scenario to work.

2. So what’s the Solution?

There are a number of discrete actions.

  • Firstly define variables for your integration test source and target directories
  • Use ant to create the output directory for the integration tests
  • Use the build helper plugin to add your test source to the compile path and copy any resources.
  • Compile the integration tests using -d to specify the path to the integration test source
  • Use the failsafe plugin to run the tests using parameters to specify the correct directories
  • Put it all in a profile so it can be used conveniently

These are shown in the lifecycle diagram below.

2.1 Define 2 Variables pointing to the integration tests

We need to define where the integration test classes are and where to compile and run them from.


2.2 Create The Output Directory

Use the Maven antrun plugin to create the output directory \target\integrationtest-classes This is needed for the compiler which won't create it!

<echo message="Creating Directory ${integrationOutputDirectory}"/>
<mkdir dir="${integrationOutputDirectory}" />

2.3 Use Build Helper To Add The Test Source And Resources

First add the classes in src\integrationtest\java to the compile path


Copy any resources from \src\integrationtest\resource to \target\integration-classes. This is not strictly necessary to get things working but allows easy packaging if you required.


2.4. Compile The Classes Using -d to Control the location

By default your classes will be compiled to \target\classes. This forces your classes to be output to target\integrationtest-classes


2.5 Use Failsafe to run the tests and customise the locations

This runs all tests in \target\integrationtest-classes. The failsafe reports are put in \target\integrationtest-classes\failsafe-reports.


2.6 The 'IT' Profile

The implementation of this is placed in a profile ‘it'https://designbycontract.googlecode.com/svn/trunk/failsafe/pom.xml

2.7 Wrapping Up.

This is a simple solution that can be rolled out to teams even if they are not strong in Maven.By providing the profile, they only need to define their own paths.

2.7 Whats Next.

Check out how to add code coverage metrics using Sonar.


From http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}