Maven Failsafe Plugin

The Maven Failsafe Plugin is designed to run integration tests in a Maven build lifecycle. It is particularly useful for tests that require the application to be packaged and deployed before they are run. This plugin is typically used for larger integration tests or end-to-end tests that validate the functionality of the application as a whole. This guide will walk you through the setup and usage of the Maven Failsafe Plugin, complete with a practical example.

Introduction to the Maven Failsafe Plugin

The Maven Failsafe Plugin allows you to separate unit tests from integration tests. Unit tests are typically run during the test phase of the build lifecycle, while integration tests are run during the integration-test phase. This separation ensures that integration tests do not interfere with the regular unit test execution and can be run after the application is packaged and deployed.

Latest Version

As of this writing, the latest version of the Maven Failsafe Plugin is 3.0.0-M8. Using the latest version ensures access to the newest features and improvements.

Setting Up the Maven Failsafe Plugin

To use the Maven Failsafe Plugin, you need to configure it in your project's pom.xml file. Let's go through the steps to set up a Maven project with the Failsafe Plugin.

Step 1: Create a Maven Project

Run the following command to create a new Maven project:

mvn archetype:generate -DgroupId=com.example -DartifactId=failsafe-plugin-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command will generate a simple Maven project with the following structure:

failsafe-plugin-demo
|-- src
|   |-- main
|   |   `-- java
|   |       `-- com
|   |           `-- example
|   |               `-- App.java
|   `-- test
|       `-- java
|           `-- com
|               `-- example
|                   `-- AppTest.java
|-- pom.xml
`-- target

Step 2: Add Maven Failsafe Plugin Configuration

Navigate to the project directory and open the pom.xml file. Add the Maven Failsafe Plugin configuration inside the <build> section:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>failsafe-plugin-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>3.0.0-M8</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Explanation

  • <groupId>: Specifies the group ID for the Maven Failsafe Plugin.
  • <artifactId>: Specifies the Maven Failsafe Plugin.
  • <version>: The version of the plugin you are using. Ensure you use the latest version.
  • <executions>: Defines the execution of the plugin, specifying when to run the integration tests and verification.

Step 3: Create an Integration Test

Create an integration test in the src/test/java directory. This test will be run by the Failsafe Plugin. For example, create a file named AppIT.java (the convention is to use IT as a suffix for integration tests):

package com.example;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AppIT {

    @Test
    void integrationTest() {
        System.out.println("Running integration test...");
        assertTrue(true);
    }
}

Step 4: Build and Run Integration Tests

To build the project and run the integration tests, use the following command:

mvn clean verify

After the build completes, the integration tests will be run during the integration-test phase, and their results will be verified during the verify phase.

Step 5: Check Results

The output will include the results of the integration tests:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.example.AppIT
Running integration test...
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec
Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

Conclusion

The Maven Failsafe Plugin is used to run integration tests in a Maven build lifecycle. By using this plugin, you can ensure that your integration tests are run separately from your unit tests, allowing for better test organization and execution. This guide provided a complete overview of setting up and using the Maven Failsafe Plugin, along with a practical example to help you get started. With this knowledge, you can effectively run and manage your integration tests in a Maven project.

Comments