使用 JUnit 和 Maven Surefire 外掛測試 Java 類
Maven Surefire 外掛在 Maven 構建過程的測試階段或當 test
被指定為 Maven 目標時執行。以下目錄結構和最小 pom.xml
檔案將配置 Maven 以執行測試。
專案根目錄中的目錄結構:
─ project_root
├─ pom.xml
├─ src
│ ├─ main
│ │ └─ java
│ └─ test
│ └─ java
└─ target
└─ ...
pom.xml
內容:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>company-app</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
在專案的 src/test/java/com/example/app
目錄中建立一個名為 PlusTenTest.java
的檔案,其中包含以下內容:
package com.example.app;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class PlusTenTest {
@Test
public void incrementTest() {
int result = PlusTen.increment(10);
assertEquals("PlusTen.increment(10) result", 20, result);
}
}
註釋 @Test
告訴 JUnit 它應該在 Maven 構建過程的 test
階段執行 incrementTest()
作為測試。現在在 src/main/java/com/example/app
中建立 PlusTen.java
:
package com.example.app;
public class PlusTen {
public static int increment(int value) {
return value;
}
}
通過開啟命令提示符,導航到專案的根目錄並呼叫以下命令來執行測試:
mvn -Dtest=com.example.app.PlusTenTest test
Maven 將編譯該程式並在 PlusTenTest
中執行測試方法 incrementTest()
。測試將失敗,並顯示以下錯誤:
...
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec <<< FAILURE! - in com.example.app.PlusTenTest
incrementTest(com.example.app.PlusTenTest) Time elapsed: 0.004 sec <<< FAILURE!
java.lang.AssertionError: PlusTen.increment(10) result expected:<20> but was:<10>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:743)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:555)
at com.example.app.PlusTenTest.incrementTest(PlusTenTest.java:12)
Results :
Failed tests:
PlusTenTest.incrementTest:12 PlusTen.increment(10) result expected:<20> but was:<10>
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.749 s
[INFO] Finished at: 2016-09-02T20:50:42-05:00
[INFO] Final Memory: 14M/209M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project app: There are test failures.
...
Maven Surefire 外掛在專案目錄中建立一個/target/surefire-reports/
目錄,其中包含檔案 com.example.app.PlusTenTest.txt
和 TEST-com.example.app.PlusTenTest.xml
,其中包含上面輸出開頭的錯誤詳細資訊。
按照測試驅動的開發模式,修改 PlusTen.java
以使 increments()
方法正常工作:
package com.example.app;
public class PlusTen {
public static int increment(int value) {
return value + 10;
}
}
再次呼叫該命令:
mvn -Dtest=com.example.app.PlusTenTest test
測試通過:
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.028 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.753 s
[INFO] Finished at: 2016-09-02T20:55:42-05:00
[INFO] Final Memory: 17M/322M
[INFO] ------------------------------------------------------------------------
恭喜! 你已使用 JUnit 和 Maven Surefire 外掛測試了 Java 類。