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”時執行此測試。

或者,您可以使用 values 列表(具有 OR 語義)配置 @IfProfileValue,以在 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 註解配置的 profile 值時要使用的 ProfileValueSource 型別。如果測試未宣告 @ProfileValueSourceConfiguration,則預設使用 SystemProfileValueSource。以下示例展示瞭如何使用 @ProfileValueSourceConfiguration

  • Java

  • Kotlin

@ProfileValueSourceConfiguration(CustomProfileValueSource.class) (1)
public class CustomProfileValueSourceTests {
	// class body...
}
1 使用自定義的 profile value 源。
@ProfileValueSourceConfiguration(CustomProfileValueSource::class) (1)
class CustomProfileValueSourceTests {
	// class body...
}
1 使用自定義的 profile value 源。

@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 將此測試重複十次。