Trong bài viết trước, mình đã nói về plugin Maven Surefire Plugin của Apache Maven để chạy Unit Test trong các project Maven. Mình đã sử dụng JUnit 4 trong ví dụ của bài viết này. Nếu bây giờ mình upgrade thư viện JUnit từ phiên bản 4 lên phiên bản 5 trong ví dụ của bài viết này bằng cách thay đổi dependency của JUnit cho phiên bản 5 trong tập tin pom.xml:
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> |
và sửa lại class CalculationTest cho đúng:
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)); } } |
thì khi click chuột phải vào project rồi chọn Run As và chọn Maven test, các bạn sẽ thấy lỗi khi chạy như sau:
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 |
Nguyên nhân là gì? Đó là bởi hiện tại Maven Surefire Plugin mặc định của phiên bản Apache Maven mà mình đang sử dụng chưa hỗ trợ đầy đủ cho JUnit phiên bản 5.
Để khắc phục lỗi này, các bạn hãy khai báo tường minh plugin Maven Surefire Plugin trong tập tin pom.xml với phiên bản từ 2.22.0 trở lên như sau:
1 2 3 4 |
<plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.0</version> </plugin> |
Lúc này, nếu chạy lại “mvn test”, các bạn sẽ không thấy lỗi nữa:
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] ------------------------------------------------------------------------ |