Plugin Goal
To better understand the goal of Surefire, let us first understand what unit testing is. Whenever we develop a large software application, we try to build it in different independent units. These units are generally called modules. When the coding of a module is completed, we test its complete code and functionality to ensure there are no errors. This testing of individual units rather than the whole project is called unit testing.
In Maven, this task is done by Surefire.
The only goal of Maven Surefire is to run unit tests of a Maven project during the testing phase. We can find the reports generated by this plugin in the surefire-reports directory inside the target folder.
Usage of Surefire Plugin
Let us learn how to use Surefire to test our application.
Maven suggests that it is better to define the version of the plugin that we want to use inside the pom.xml file. POM stands for project object model. The POM file contains information about the project and its configurations. This file is the place where we code our Surefire plugin.
We define the plugin version inside the <version> element. The latest version of Surefire available is 3.0.0-M7. Apache released this version in June 2022.
Let us see the code:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
As you may already know, we define all plugins in the <plugin> elements section inside the <build> element in the POM file.
Now to call this plugin, we only need to run the test phase of the build lifecycle. We can do so by typing the following command in the command line:
mvn test
Configuration
Surefire is a user-friendly and universal plugin. It works fine with several testing frameworks, such as JUnit, TestNG, and POJO. We do not need to add any extra configurations to specify the provider.
There is one problematic feature of Surefire that is present as default. This plugin automatically includes all the public test cases containing the word “test.” Now, there may be situations where you do not want to include all such files for testing. It is easy to handle such situations. All you need to do is use <includes> and <excludes> parameters while defining the plugin.
The <include> parameter includes all the files mentioned inside it for unit testing. Similarly, the <exclude> parameter excludes the files from the testing process. Consider the following example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<excludes>
<exclude>Demofile1.java</exclude>
</excludes>
<includes>
<include>Demofile2.java</include>
</includes>
</configuration>
</plugin>
Here, Demofile1.java will be excluded from testing, while Demofile2.java will be tested.
Examples
Hey Ninja, just a reminder that you are doing great! You have learned how to use Surefire and its configurations. This is good progress. Let us now look at some examples to understand the need for Surefire.
Running a Single Test Case
You may want to run a particular test class repeatedly during the testing phase. You can do this as
mvn surefire: test -Dtest=TestCircle
Here, the test parameter takes the test class as input.
Skipping Tests
We can set the skipTests property as true in the POM file of a project. This property will exclude that project from unit testing.
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
We also do it directly by typing mvn install -Dmaven.test.skip=true in the Maven command line.
Must Read Apache Server
Frequently Asked Questions
How can I use Maven features in an Ant build?
We can use the features of Maven in an ant build using the Maven Ant Tasks. These tasks allow features such as repository deployment and resource management to be used in an ant build.
Can I create my own directory structure in Maven?
Yes! You can easily create your own directory structure in Maven. You just need to configure some elements of the <build> sections, such as the source directory, resources, etc.
What do you mean by POM in Maven?
POM stands for Project Object Model. POM is basically an XML file that stores the configuration details of a project in Maven. These details can be plugins, goals, dependencies, etc.
Conclusion
Hey Ninja! We hope you now understand Maven Surefire. We started by understanding the basics of Maven Surefire. We then learned about its goal and also discussed what unit testing is. After that, we looked at its usage and learned how to configure it.
We hope the above discussion helped you understand Maven’s Surefire plugin and its importance in Java project development. If you wish to learn more about Maven and its various components, you can refer to blogs on maven setup, configuration, and interview questions.
Do visit our website to read more such blogs. Make sure you enroll in our courses. You can take mock tests, solve problems, and interview puzzles. Also, you can check out some exciting interview stuff- interview experiences and an interview bundle for placement preparations. Do upvote our blog to help fellow ninjas grow.
Keep Grinding! 🦾
Happy Coding! 💻