Wednesday, September 16, 2009

Maven Surefire for Integration Tests in JUnit

It is quite convenient to use maven-surefire-plugin to run tests using Maven. It enables generating reports in txt, XML as well as HTML. By default surefire tests are executed in the test phase of Maven build lifecycle. Therefore it enables to run unit tests, but not integration tests which should be executed after package phase.
It is just a matter of configuring the surefire plugin, in order to run integration tests. The configuration given below can be used to change the phase of the surefire plugin.

<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-surefire-plugin</artifactid>
<configuration>
<!-- skip unit tests in the test phase, since it is required to run integration tests -->
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>surefire-it</id>
<phase>integration-test</phase>
<configuration>
<skip>false</skip>
<!--forkMode>pertest</forkMode-->
<argline>-enableassertions</argline>
<testfailureignore>true</testfailureignore>
</configuration>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<plugin>
In addition, we need to add the junit dependancy to the pom's dependancy list. This should be added in a manner that it resolves only for the testing.

<dependencies&
...

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

</dependencies>
The version may change depending on your requirements.