Updated February 20, 2023
Introduction to Maven Failsafe Plugin
Maven failsafe plugin is a part of maven plugins used for running the integration test of our maven project. The failsafe plugin is similar to the plugin of maven surefire. The failsafe plugin is used in the integration tests, whereas the surefire plugin runs the unit test for the project. The failsafe plugin is very important when doing integration testing.
Overview of Maven Failsafe Plugin
When running the integration test, the lifecycle will contain the same four phases. Below are the lifecycle phases for running the integration tests as follows.
- The first phase is the pre-integration test, which was used to set up the integration test environment.
- The second phase of the life cycle of the integration test is the integration test used to run the integration testing.
- The third phase of the life cycle of the integration test is a post-integration test used to tear down the environment of the integration test.
- The fourth phase of the life cycle of the integration test verifies the integration test, which was used to check the result of integration tests.
The maven plugin of failsafe creates the reports into two different file formats, i.e., failsafe integration tests are running on integration tests for a specific application. Failsafe verify will verify the integration test from which the application is passed.
Key Takeaways
- The failsafe plugin runs the method onto the test classes like the surefire plugin. A failsafe plugin is used to do the integration test.
- The goal of the failsafe plugin is the integration test, which was bound to the integration test phase by default.
How to Use Maven Failsafe Plugin?
We must create the project template into the spring tool suite to use the failsafe plugins. The below steps show how we can use the failsafe plugin.
- In this step, we are creating the project template of the failsafe plugin into the spring initializer. We are giving the name of the maven project as maven_failsafe. We are defining the packaging as jar and selecting the java version as 8.
Group name – com.example Artifact – maven_failsafe
Name – maven_failsafe Packaging – jar
Java version – 8
- After creating the template of maven failsafe, now in this step, we are opening the failsafe plugin project into the spring tool suite as follows.
- After opening the project template of the failsafe plugin in this step we are checking the project structure and files.
- After opening the project now in this step we are adding the failsafe plugin into the pom.xml file as follows.
Code:
<plugin>
<artifactId> maven-failsafe-plugin </artifactId>
<version> 3.0.0-M5 </version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
- After adding the plugin into the pom.xml file now in this we are building the project through GUI as follows.
Maven Failsafe Run
The failsafe plugin is used to run the test. In the example below, we are configuring the jetty server to start the integration test and stop the same after the test execution. To start the integration tests first, we need to add the plugin of a jetty server into the pom.xml file. In the example below, we are first adding the code below into the pom.xml file. In the below example, we are defining the plugin directive.
Code:
<plugin>
<groupId> org.eclipse.jetty </groupId>
<artifactId> maven-plugin </artifactId>
<version> 9.4.11.v20180605 </version>
<executions>
<execution>
<id> start </id>
<phase> integration test </phase>
<goals>
<goal> start </goal>
</goals>
</execution>
<execution>
<id> stop </id>
<phase> post test </phase>
<goals>
<goal> stop </goal>
</goals>
</execution>
</executions>
</plugin>
After adding the plugin of a jetty, below we are running the integration test on the jetty plugin as follows. In the above example, we added the configuration to start and stop the jetty server at the time of the integration test. In the below example, we are executing the test and checking the result from the console. In the below example we can see that we have run the integration test on the jetty plugin. For using the different plugins for different tests it is nothing but the separation between configurations.
Setting up Maven Failsafe Plugin
At the time of setting up the failsafe plugin first, we need to configure the failsafe plugin into the pom.xml file. All the plugins we are adding to the project need to be added to the pom.xml file. We are setting up the below configuration into the pom.xml file. We are defining the artifact id as “maven failsafe setting,” then we are defining the version of configuration as 3.0.0. Also, we are defining the goal as an integration test and verifying it as follows.
Code:
<plugin>
<artifactId> maven-plugin </artifactId>
<version>3.0.0-M5</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
After setting up the failsafe configuration now we are running the integration test and building the configuration of the failsafe plugin as follows.
Maven Failsafe Plugin Exclusion
The failsafe plugin runs on the test classes of the surefire plugin. We are configuring the surefire and failsafe plugins similarly. The failsafe plugin’s goal will be to specify in the pom.xml file. The below example shows failsafe plugin execution as follows. In the below example, we are defining the integration test as follows.
Code:
<plugin>
<artifactId> maven-failsafe </artifactId>
<version> 2.21.0 </version>
<executions>
<execution>
<goals>
<goal> test </goal>
</goals>
<configuration>
</configuration>
</execution>
</executions>
</plugin>
After defining the exclusion of the maven failsafe plugin below example we are loading the failsafe plugin as follows.
FAQ
Given below are the FAQs mentioned:
Q1. What is the use of the failsafe plugin a maven application?
Answer: We are using the failsafe plugin to do the integration test of the maven project. It works similarly to the surefire plugin.
Q2. How many goals are defined in failsafe plugin?
Answer: There are two types of goals defined in the failsafe plugin i.e. verify and the integration test.
Q3. Can we use a jetty server to run the integration test by using the failsafe plugin?
Answer: To run the integration test of the project we are using a jetty server to run the test of the maven application.
Conclusion
When running the integration test, the lifecycle contains the four phases of the maven failsafe plugin. Maven failsafe plugin is a part of plugins used to run integration tests of our project. The maven failsafe plugin is similar to the plugin of maven surefire.
Recommended Articles
This is a guide to Maven Failsafe Plugin. Here we discuss the introduction and how to use Maven Failsafe Plugin to run, setting up, and exclusion. You may also have a look at the following articles to learn more –