Spring JUnit 4 測試註解
|
自 Spring Framework 7.0 起,JUnit 4 支援已被棄用,取而代之的是 |
以下註解僅在使用 SpringRunner、Spring 的 JUnit 4 規則 或 Spring 的 JUnit 4 支援類時受支援
@IfProfileValue
@IfProfileValue 指示帶註解的測試類或測試方法在特定的測試環境中是否啟用。如果配置的 ProfileValueSource 為提供的 name 返回匹配的 value,則測試啟用。否則,測試將被停用,並被有效地忽略。
您可以將 @IfProfileValue 應用到類級別、方法級別或兩者。類級別的 @IfProfileValue 用法對於該類或其子類中的任何方法具有優先權。具體來說,如果測試在類級別和方法級別都被啟用,則測試啟用。缺少 @IfProfileValue 意味著測試隱式啟用。這類似於 JUnit 4 的 @Ignore 註解的語義,只是 @Ignore 的存在總是停用測試。
以下示例展示了一個帶有 @IfProfileValue 註解的測試
-
Java
-
Kotlin
@IfProfileValue(name="java.vendor", value="Oracle Corporation") (1)
@Test
public void testProcessWhichRunsOnlyOnOracleJvm() {
// some logic that should run only on Java VMs from Oracle Corporation
}
| 1 | 僅當 Java 供應商為 "Oracle Corporation" 時執行此測試。 |
@IfProfileValue(name="java.vendor", value="Oracle Corporation") (1)
@Test
fun testProcessWhichRunsOnlyOnOracleJvm() {
// some logic that should run only on Java VMs from Oracle Corporation
}
| 1 | 僅當 Java 供應商為 "Oracle Corporation" 時執行此測試。 |
或者,您可以將 @IfProfileValue 配置為包含 values 列表(具有 OR 語義),以在 JUnit 4 環境中實現類似 TestNG 的測試組支援。請考慮以下示例
-
Java
-
Kotlin
@IfProfileValue(name="test-groups", values={"unit-tests", "integration-tests"}) (1)
@Test
public void testProcessWhichRunsForUnitOrIntegrationTestGroups() {
// some logic that should run only for unit and integration test groups
}
| 1 | 執行此測試用於單元測試和整合測試。 |
@IfProfileValue(name="test-groups", values=["unit-tests", "integration-tests"]) (1)
@Test
fun testProcessWhichRunsForUnitOrIntegrationTestGroups() {
// some logic that should run only for unit and integration test groups
}
| 1 | 執行此測試用於單元測試和整合測試。 |
@ProfileValueSourceConfiguration
@ProfileValueSourceConfiguration 是一個可應用於測試類的註解,用於指定在透過 @IfProfileValue 註解檢索配置檔案值時使用哪種型別的 ProfileValueSource。如果未為測試宣告 @ProfileValueSourceConfiguration,則預設使用 SystemProfileValueSource。以下示例展示瞭如何使用 @ProfileValueSourceConfiguration
-
Java
-
Kotlin
@ProfileValueSourceConfiguration(CustomProfileValueSource.class) (1)
public class CustomProfileValueSourceTests {
// class body...
}
| 1 | 使用自定義配置檔案值源。 |
@ProfileValueSourceConfiguration(CustomProfileValueSource::class) (1)
class CustomProfileValueSourceTests {
// class body...
}
| 1 | 使用自定義配置檔案值源。 |
@Timed
@Timed 指示帶註解的測試方法必須在指定的時間段(以毫秒為單位)內完成執行。如果文字執行時間超過指定時間段,則測試失敗。
時間段包括執行測試方法本身、測試的任何重複(參見 @Repeat)以及測試裝置的任何設定或拆卸。以下示例展示瞭如何使用它
-
Java
-
Kotlin
@Timed(millis = 1000) (1)
public void testProcessWithOneSecondTimeout() {
// some logic that should not take longer than 1 second to run
}
| 1 | 將測試的時間段設定為一秒。 |
@Timed(millis = 1000) (1)
fun testProcessWithOneSecondTimeout() {
// some logic that should not take longer than 1 second to run
}
| 1 | 將測試的時間段設定為一秒。 |
Spring 的 @Timed 註解與 JUnit 4 的 @Test(timeout=…) 支援具有不同的語義。具體來說,由於 JUnit 4 處理測試執行超時的方式(即,在單獨的 Thread 中執行測試方法),如果測試耗時過長,@Test(timeout=…) 會搶先使測試失敗。另一方面,Spring 的 @Timed 不會搶先使測試失敗,而是在測試完成之後才失敗。
@Repeat
@Repeat 指示帶註解的測試方法必須重複執行。測試方法執行的次數在註解中指定。
重複執行的範圍包括測試方法本身的執行以及測試裝置的任何設定或拆卸。與 SpringMethodRule 一起使用時,範圍還包括由 TestExecutionListener 實現對測試例項的準備。以下示例展示瞭如何使用 @Repeat 註解
-
Java
-
Kotlin
@Repeat(10) (1)
@Test
public void testProcessRepeatedly() {
// ...
}
| 1 | 重複此測試十次。 |
@Repeat(10) (1)
@Test
fun testProcessRepeatedly() {
// ...
}
| 1 | 重複此測試十次。 |