EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Maven Tutorial Maven Surefire
Secondary Sidebar
Maven Tutorial
  • Maven Basic and advance
    • What is Maven
    • How to Install Maven
    • Maven Commands
    • Maven GroupId
    • Maven WAR Plugin
    • Maven Build Command
    • Maven Failsafe Plugin
    • Maven Profile
    • IntelliJ Maven
    • Maven Enforcer Plugin
    • Maven Javadoc Plugin
    • Maven WAR Plugin
    • Maven Build Command
    • Maven GroupId
    • Maven Force Update
    • Maven Encrypt Password
    • Maven Environment Variables
    • AssertJ Maven
    • Maven Run Single Test
    • Maven kafka-clients
    • Maven Quickstart Archetype
    • Maven Install Dependencies
    • Maven XMLBeans
    • Maven Local Repository
    • Maven Versions
    • Maven Jar Plugin
    • Maven Assembly Plugin
    • Maven exec plugin
    • Maven Central Repository
    • Maven Surefire
    • Maven Deploy
    • Maven Phases
    • Maven Archetype
    • Maven Skip Test
    • Maven Dependency Scope
    • Maven Shade Plugin
    • Maven Repository Spring
    • Maven Eclipse Plugin
    • Maven Exclude Dependency
    • Maven Life Cycle
    • Maven Repository
    • Maven POM File
    • Maven Plugins
    • What is Maven Plugins
    • Maven Interview Questions
    • Maven Flags
    • Maven Project
    • Maven Settings.XML

Maven Surefire

By Payal UdhaniPayal Udhani

Maven Surefire

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,697 ratings)
  • 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.

maven surefire 1

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.

maven surefire 2

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.

test class can be created

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.

select the methods you want to add

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 surefire 5

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.

maven surefire 6

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.

maven surefire 7

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.

green bar shows that the test case is successfully executed

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

This is a guide to Maven Surefire. Here we discuss the introduction to Maven Surefire, prerequisites, behavior, compatibility with different test providers, modules framework and example. You may also have a look at the following articles to learn more –

  1. Maven Life Cycle
  2. Maven Repository
  3. Maven POM File
  4. Maven Plugins
Popular Course in this category
Maven Training (4 Courses, 6 Projects)
  4 Online Courses |  6 Hands-on Project |  26+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more