Maven Failsafe Plugin

1. Overview

The Failsafe Plugin is designed to run integration tests while the Surefire Plugin is designed to run unit tests.
The Maven lifecycle has four phases for running integration tests:
  • pre-integration-test for setting up the integration test environment.
  • integration-test for running the integration tests.
  • post-integration-test for tearing down the integration test environment.
  • verify for checking the results of the integration tests. If you use the Surefire Plugin for running tests, then when you have a test failure, the build will stop at the integration-test phase and your integration test environment will not have been torn down correctly.
The Failsafe Plugin generates reports in two different file formats:
  • Plain text files (*.txt)
  • XML files (.xml) By default, these files are generated in ${basedir}/target/failsafe-reports/TEST-.xml.

2. Plugin Goals

The Failsafe Plugin has only two goals:
  • failsafe:integration-test runs the integration tests of an application.
  • failsafe:verify verifies that the integration tests of an application passed.

3. Usage

To use the Failsafe Plugin, you need to add the following configuration to your pom.xml:
<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.22.0</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>
The Failsafe Plugin can be invoked by calling the verify phase of the build lifecycle.
mvn verify

4. Examples

Manually Specifying a Provider You can also manually force a specific provider by adding it as a dependency to Failsafe itself:
<plugins>
[...]
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.22.0</version>
    <dependencies>
      <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-junit47</artifactId>
        <version>2.22.0</version>
      </dependency>
    </dependencies>
  </plugin>
[...]
</plugins>
When using this technique there is no check that the proper test-frameworks are present on your project's classpath. Failing to add the proper test-frameworks will result in a build failure.

Running Tests in Parallel

From JUnit 4.7 onwards you can run your tests in parallel. To do this, you must set the parallel parameter, and may change the threadCount or useUnlimitedThreads attribute. For example:
<plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.22.0</version>
        <configuration>
          <parallel>methods</parallel>
          <threadCount>10</threadCount>
        </configuration>
      </plugin>
    [...]
</plugins>

4. Conclusion

In this quick guide, we went over the Failsafe plugin and gave instructions on using and customizing it. Also we have seen the different plugin goals and their usage.

Comments