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 PluginStep 1: Usage of Surefire PluginMaven 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 PluginThe 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 CaseYou 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 PluginWe 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.
|