Updated April 12, 2023
Introduction to Maven Surefire
Apache Maven is a building tool that also supports other operations for project management such as dependency management, documentation and unit testing. The build lifecycle of the maven is divided into multiple phases namely validate, compile, test, package, verify, install, and deploy. Maven surefire is a plugin that is used in the test phase of the build lifecycle of maven. This plugin helps in making the execution of unit test cases of a maven project or application and generate the reports of the testing results in the test phase of the build lifecycle. This plugin creates the reports in two different formats namely plain test files with .txt extension and XML files with .xml extension.
Modules of Surefire Testing Framework
Given below are the various modules framework:
- SureFire Logger
- SureFire API
- Surefire Extensions
- SureFireBooter
- Maven Surefire Test-Grouping Support
- SureFire Providers
- ShadeFire JUnit3 Provider
- Maven Surefire Common
- Surefire Report Parser
- Maven Surefire Plugin
- Maven Failsafe Plugin
- Maven Surefire Report Plugin
- Maven Surefire Integration Tests
- Surefire Shared Utils
Prerequisites
There are many releases of the surefire plugin with developing versions of maven. However, for latest surefire release that corrected the formatting of test messages on console that were incorrect in versions of maven before 3.1.0 following are the suggested requirements.
- Maven 3.1.0 or above maven 3.x version.
- JDK (Java Development Toolkit) with version 1.7 or higher.
Behavior
- The surefire plugins help to export the reports of unit testing in plain text or XML format.
- It can also be exported in HTML by taking some extra efforts.
- The default path where the generated reports of surefire plugin are stored is /target/surefire-reports/AnyNameOfFile-*(.xml/.txt).
- This surefire plugin has one goal defined for it that is surefire, test that specifies to run the unit tests of the maven project/application.
Compatibility with Different Test Providers
Maven surefire plugin works completely fine with any of the following combinations of the test source directories content.
- Junit(5.x,3.8 or 4.x version)
- POJO(Plain Old Java Object)
- TestNG
There is no need for any extra configurations to specify which provider is used and available for the test cases in the maven project. This is incurred from the dependencies that you add in your pom.xml file.
For example, if you are using Junit 5 for testing, then there will be a dependency tag with JUnit 5 ‘s group id artifact id and version in the dependencies element of your pom.xml file of the project.
How to use?
One of the most commonly used and preferred methods of using this plugin is to specify the version of this plugin in the plugins element of your pom.xml file or parent pom.xml file of your maven project.
Code:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
This plugin tag should be added in plugins elements inside the pluginManagement tag of your pom.xml. 3.0.0-M4 is the latest version of the surefire plugin.
An alternative way to use the maven surefire plugin is to call the test phase of the maven builds lifecycle which will invoke this plugin automatically. The test phase of maven lifecycle of building a project can be called by using the following command:
Code:
mvn test
The surefire maven plugin includes all the test classes that are public and contain the word test in it whether the test word be situated in beginning or ending of the class name. However, this behavior can be changed with the help of excludes and includes parameters in configuration in the following way.
Code:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<excludes>
<exclude>SampleTest.java</exclude>
</excludes>
<includes>
<include>NeedToVerify.java</include>
</includes>
</configuration>
</plugin>
This will lead to exclusion of SampleTest file for unit testing and will include NeedToVerify class file for unit testing even when it does not contains the test word in its name because it was mentioned in include element of configuration element in pom.xml file.
Example of Maven Surefire
Given below is the example mentioned:
Begin by creating a new maven project.
Consider the following example where we have two methods, one for calculating product and other for division in the class named Arithmetic. Create a new java class file named Arithmetic.
Code:
public class Arithmetic {
public intproduct(int number1, int number2) {
return number1 * number2;
}
public intdivision(int number1, int number2) {
return number1 / number2;
}
}
This class file should be structured and located in the following way.
Now, we will create a test class named ArithmeticTest according to coding conventions the name of the testing class should be the name of the class which is being tested followed by the Test keyword. This will contain test methods whose naming convention is the name of the method being test prepended with test word. Hence, here are two testing methods named testProduct and testDivision.
The test class can be created by clicking on new -> other and then the test case option inside JUnit as shown below.
Then select the methods you want to add in the test class file and mention the name of the test class file and the file being tested in the following window to create a test class file.
Code:
import org.junit.*;
public class ArithmeticTest {
@BeforeClass
public static void setUpClass() {
// This block is executed before all the methods of test
}
@Before
public void setUp() {
// This block is executed before each method of test
}
@Test
public void testProduct() {
// This block is executed for testing product method of Arithmetic class
Arithmetic arithmetic = new Arithmetic();
int number1 = 100;
int number2 = 5;
intactualvalue = arithmetic.product(number1, number2);
intexpectedvalue = 500;
assertEquals(expectedvalue, actualvalue);
}
@Test
public void testDivision() {
// This block is executed for testing division method of Arithmetic class
}
@After
public void tearDown() {
// This block is executed after each method of test
}
@AfterClass
public static void tearDownClass() {
// This block is executed after all the methods of test
}
}
Note that all the methods above that are annotated with BeforeClass, Before, After and AfterClass are optional. Only test methods are required in test classes. Each method has its specific purpose and the time when it is executed.
This ArithmeticTest class file should be structured and located in the following way.
Maven’s pom.xml will contain.
Code:
<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.educba</groupId>
<artifactId>JUnit-testing-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
</build>
</project>
This file should be structured and present in the following way.
To check the test cases and execute the test, open the outline window from the window -> preferences -> outline and then right-click on any method of test class file you want to execute and run as the JUnit test option. After running the above testProduct method of ArithmeticTest class you will get the following output on Junit console.
The green bar shows that the test case is successfully executed and there are no errors. For running whole class file of test cases you can right-click the ArithmeticClass file and run a Junit test that gives following output.
Conclusion
We can test the maven projects with the help of surefire plugin and we can perform unit testing for multiple test cases and with any of the underlying source directories content like Junit, POJO(Plain Old Java Object), TestNG.
Recommended Articles
We hope that this EDUCBA information on “Maven Surefire” was beneficial to you. You can view EDUCBA’s recommended articles for more information.