The Maven Surefire Plugin is a core Apache Maven plugin that allows us to run the Maven project’s Unit Test. It can also generate reports to help you see how many test passes, how many test failures, and what the causes are for the Unit Test. How is it in details? Let’s learn more about it in this tutorial.
I will create a new Maven project with a simple example with Unit Test as follows:
JUnit dependency:
1 2 3 4 5 6 |
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> |
Calculation class:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.huongdanjava.mavensurefireplugin; public class Calculation { public int add(int a, int b) { return a + b; } public int sub(int a, int b) { return a - b; } } |
CalculationTest class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.huongdanjava.mavensurefireplugin; import static org.junit.Assert.assertEquals; import org.junit.Before; import org.junit.Test; public class CalculationTest { private Calculation calculation; @Before public void init() { calculation = new Calculation(); } @Test public void testAdd() { assertEquals(4, calculation.add(1, 3)); } @Test public void testSub() { assertEquals(2, calculation.sub(7, 5)); } } |
Now, in Eclipse, if you right-click on the project, select Run As and then select Maven test, and you’ll see the results in the Eclipse Console tab:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building maven-surefire-plugin 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-surefire-plugin --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ maven-surefire-plugin --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-surefire-plugin --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ maven-surefire-plugin --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-surefire-plugin --- [INFO] Surefire report directory: /Users/khanh/Working/Code/huongdanjava.com/maven-surefire-plugin/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.huongdanjava.mavensurefireplugin.CalculationTest Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.069 sec Results : Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.641 s [INFO] Finished at: 2018-10-10T02:32:21+07:00 [INFO] Final Memory: 10M/155M [INFO] ------------------------------------------------------------------------ |
Obviously, as you can see, although I did not declare anything related to the Maven Surefire Plugin, it was still called and ran with the “mvn test” Apache Maven command. That’s because Maven Surefire Plugin is the core plugin for Apache Maven.
Maven Surefire Plugin can work with testing frameworks like JUnit or TestNG. No matter how you use the testing framework, the results of the Unit Test remain the same.
This plugin has only one Maven goal, a “test”. This goal will be run by default during the test phase of the lifecycle build process or when you run the “mvn test” command.
By default, all classes with the name starting with Test or ending with Test, Tests, and TestCase will be picked up to run the Unit Test. As you can see in the example above, the Maven Surefire Plugin automatically picked up the CalculationTest class to run the Unit Test. If you now rename this class to the name not unsatisfactory above conditions, you will not see the Unit Test for the Calculation class being run.
As I said at the beginning, after running the unit test, the Maven Surefire Plugin will generate reports in the target/surefire-reports folder of the project to record the Unit Test results.
As you can see, there are two file formats that Maven Surefire Plugin will generate: .txt and .xml.
The Maven Surefire Plugin also allows us to exclude the Unit Test classes that we do not want to run or include classes that write Unit Tests, but the names of these classes do not meet the requirements above. To do this, you have to declare this plugin to your pom.xml file. For example, I exclude, does not run CalculationTest class like this:
1 2 3 4 5 6 7 8 9 |
<plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.0</version> <configuration> <excludes> <exclude>CalculationTest.java</exclude> </excludes> </configuration> </plugin> |
Result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building maven-surefire-plugin 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-surefire-plugin --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ maven-surefire-plugin --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-surefire-plugin --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ maven-surefire-plugin --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.22.0:test (default-test) @ maven-surefire-plugin --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.896 s [INFO] Finished at: 2018-10-10T02:34:24+07:00 [INFO] Final Memory: 10M/155M [INFO] ------------------------------------------------------------------------ |