In my previous tutorial, I talked about the Apache Maven’s Maven Surefire Plugin plugin to run Unit Test in Maven projects. I used JUnit 4 in the example of this tutorial. If I now upgrade my JUnit library from version 4 to version 5 in that example by changing the dependency of JUnit for version 5 in the pom.xml file:
1 2 3 4 5 6 |
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.3.1</version> <scope>test</scope> </dependency> |
and correct the CalculationTest class properly:
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 |
package com.huongdanjava.mavensurefireplugin; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance.Lifecycle; @TestInstance(Lifecycle.PER_CLASS) public class CalculationTest { private Calculation calculation; @BeforeAll 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)); } } |
then when you right click on the project and select Run As and select Maven test, you will see the error when running as follows:
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
[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: 2, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec <<< FAILURE! com.huongdanjava.mavensurefireplugin.CalculationTest.testAdd() Time elapsed: 0.002 sec <<< FAILURE! java.lang.NullPointerException at com.huongdanjava.mavensurefireplugin.CalculationTest.testAdd(CalculationTest.java:19) com.huongdanjava.mavensurefireplugin.CalculationTest.testSub() Time elapsed: 0 sec <<< FAILURE! java.lang.NullPointerException at com.huongdanjava.mavensurefireplugin.CalculationTest.testSub(CalculationTest.java:24) Results : Failed tests: com.huongdanjava.mavensurefireplugin.CalculationTest.testAdd() com.huongdanjava.mavensurefireplugin.CalculationTest.testSub() Tests run: 2, Failures: 2, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.392 s [INFO] Finished at: 2018-10-10T02:57:00+07:00 [INFO] Final Memory: 10M/155M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project maven-surefire-plugin: There are test failures. [ERROR] [ERROR] Please refer to /Users/khanh/Working/Code/huongdanjava.com/maven-surefire-plugin/target/surefire-reports for the individual test results. [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException |
What are the causes? That’s because the current Maven Surefire Plugin default of the Apache Maven version that I’m using is not fully supported for JUnit version 5.
To overcome this error, you explicitly declare Maven Surefire Plugin plugin in the pom.xml file with version 2.22.0 or above as follows:
1 2 3 4 |
<plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.0</version> </plugin> |
At this point, if you run “mvn test”, you will not see the error again:
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 |
[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] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.huongdanjava.mavensurefireplugin.CalculationTest [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.011 s - in com.huongdanjava.mavensurefireplugin.CalculationTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.128 s [INFO] Finished at: 2018-10-10T03:04:46+07:00 [INFO] Final Memory: 12M/220M [INFO] ------------------------------------------------------------------------ |