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
),以及測試 fixture 的任何設定或拆解。以下範例顯示如何使用它
-
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
指出已註解的測試方法必須重複執行。測試方法要執行的次數在註解中指定。
要重複的執行範圍包括測試方法本身的執行以及測試 fixture 的任何設定或拆解。與 SpringMethodRule
搭配使用時,範圍還額外包括 TestExecutionListener
實作對測試實例的準備。以下範例顯示如何使用 @Repeat
註解
-
Java
-
Kotlin
@Repeat(10) (1)
@Test
public void testProcessRepeatedly() {
// ...
}
1 | 將此測試重複十次。 |
@Repeat(10) (1)
@Test
fun testProcessRepeatedly() {
// ...
}
1 | 將此測試重複十次。 |