Maven Surefire is a plugin that runs the unit tests for a Maven application or project. It is used throughout the Maven build lifecycle's testing step. After testing, this plugin creates a unit test report. It can create reports in two formats that are, plain text and XML. The XML format for report production is the default mode. The plain text format has the .txt extension, but the XML format has the .xml extension.
Implementation of the Maven Surefire Plugin
Step 1: Usage of Surefire Plugin
Maven proposes defining the version of the plugin we wish to use in the pom.xml file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version> 3.1.0- M7</version>
</plugin>
Step 2: Configuration of Surefire Plugin
The Surefire plugin is compatible with the JUnit and TestNG test frameworks. Surefire's behavior remains consistent regardless of the framework used.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<!-- List of test classes to exclude from the test run -->
<excludes>
<exclude>DataTest.java</exclude> <!-- This file will be excluded from the test execution -->
</excludes>
<!-- List of test classes to include in the test run -->
<includes>
<include>DataCheck.java</include> <!-- Only this file will be included in the test execution -->
</includes>
</configuration>
</plugin>
- It is simple to handle such circumstances, use the <includes> and <excludes> arguments to define the plugin.
- The <include> parameter includes all the files named within it for unit testing.
- Similarly, the <exclude> argument excludes files from the testing procedure.
Step 3: Run a Single Test Case
You may want to run a specific test class multiple times during the testing phase.
mvn surefire:test -Dtest=TestCircle
Step 4: Skipping Tests on the Surefire Plugin
We can set the skipTests property to true in a project's POM file. This attribute will keep that project out of unit testing.
XML
<project>
<build>
<plugins>
<!-- Surefire Plugin Configuration -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.0- M7</version>
<configuration>
<skipTests>true</skipTests> <!-- Skip tests during the build -->
</configuration>
</plugin>
<!-- Checkstyle Plugin Configuration -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<configLocation>checkstyle.xml</configLocation> <!-- Path to your Checkstyle configuration -->
<failOnViolation>true</failOnViolation> <!-- Fail the build if violations are found -->
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- PMD Plugin Configuration -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<rulesets>
<ruleset>/rulesets/java/quickstart.xml</ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
The implementation provided set up the Maven Surefire plugin for running and managing tests.
Similar Reads
Maven Plugins Maven plugins are essential components in the Apache Maven build system, designed to extend its functionality. They perform tasks such as compilation, testing, packaging, deployment, and others. Maven plugins are divided into two main types:Build PluginsReporting Plugins1. Build PluginsThese plugins
2 min read
Plugins in Figma Plugins in Figma are powerful tools that extend the design platform's capabilities, allowing users to optimize their workflow, automate activities, and access new features. From prototyping to animation, there is a plugin for practically every design requirement, increasing creativity and efficiency
8 min read
Maven Compiler Plugin The Maven Compiler Plugin helps you to compile Java source code. This plugin compiles your project's sources. Since version 3.0, the default compiler is javax.tools.JavaCompiler, which compiles Java sources. The compile goal is associated with the compile phase and is used to compile the primary sou
3 min read
8 Must Have WooCommerce Security Plugins Security is essential for all kinds of websites, and even more so for e-commerce ones where users have to submit sensitive information like personal data, credit/debit/bank details as well as addresses. Although WordPress and WooCommerce have built-in security measures that are pretty decent for sta
8 min read
WordPress Plugin Setting Setting up your WordPress plugins correctly makes your website run smoothly and efficiently. WordPress plugins are one of the most important parts of WordPress it is just like the apps on mobile phones which add extra functionality to your mobile phone like WordPress plugins add new and more feature
6 min read
Maven Tutorial Maven is a powerful project management and build automation tool, widely used by Java developers. It uses a Project Object Model (POM) to centralize and handle a project's build process, reporting, and documentation efficiently. Unlike other tools like ANT, Maven offers more advanced features, makin
8 min read