@DirtiesContext
@DirtiesContext
表示底層的 Spring ApplicationContext
在測試執行期間已被弄髒 (也就是說,測試以某種方式修改或損壞了它 — 例如,透過變更 singleton bean 的狀態),應關閉。當應用程式 context 被標記為髒汙時,它會從測試框架的快取中移除並關閉。因此,對於任何後續需要具有相同組態中繼資料之 context 的測試,都會重建底層的 Spring 容器。
您可以在同一個測試類別或測試類別階層中使用 @DirtiesContext
作為類別層級和方法層級註解。在這種情況下,ApplicationContext
會在任何此類註解方法之前或之後,以及在目前測試類別之前或之後被標記為髒汙,具體取決於已組態的 methodMode
和 classMode
。當 @DirtiesContext
在類別層級和方法層級都宣告時,這兩個註解的已組態模式都會被遵循。例如,如果類別模式設定為 BEFORE_EACH_TEST_METHOD
且方法模式設定為 AFTER_METHOD
,則 context 會在給定的測試方法之前和之後都被標記為髒汙。
以下範例說明在各種組態情境中,context 何時會被弄髒
-
在目前的測試類別之前,當在類別模式設定為
BEFORE_CLASS
的類別上宣告時。-
Java
-
Kotlin
@DirtiesContext(classMode = BEFORE_CLASS) (1) class FreshContextTests { // some tests that require a new Spring container }
1 在目前的測試類別之前弄髒 context。 @DirtiesContext(classMode = BEFORE_CLASS) (1) class FreshContextTests { // some tests that require a new Spring container }
1 在目前的測試類別之前弄髒 context。 -
-
在目前的測試類別之後,當在類別模式設定為
AFTER_CLASS
的類別上宣告時 (即預設類別模式)。-
Java
-
Kotlin
@DirtiesContext (1) class ContextDirtyingTests { // some tests that result in the Spring container being dirtied }
1 在目前的測試類別之後弄髒 context。 @DirtiesContext (1) class ContextDirtyingTests { // some tests that result in the Spring container being dirtied }
1 在目前的測試類別之後弄髒 context。 -
-
在目前測試類別中的每個測試方法之前,當在類別模式設定為
BEFORE_EACH_TEST_METHOD
的類別上宣告時。-
Java
-
Kotlin
@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD) (1) class FreshContextTests { // some tests that require a new Spring container }
1 在每個測試方法之前弄髒 context。 @DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD) (1) class FreshContextTests { // some tests that require a new Spring container }
1 在每個測試方法之前弄髒 context。 -
-
在目前測試類別中的每個測試方法之後,當在類別模式設定為
AFTER_EACH_TEST_METHOD
的類別上宣告時。-
Java
-
Kotlin
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD) (1) class ContextDirtyingTests { // some tests that result in the Spring container being dirtied }
1 在每個測試方法之後弄髒 context。 @DirtiesContext(classMode = AFTER_EACH_TEST_METHOD) (1) class ContextDirtyingTests { // some tests that result in the Spring container being dirtied }
1 在每個測試方法之後弄髒 context。 -
-
在目前的測試之前,當在方法模式設定為
BEFORE_METHOD
的方法上宣告時。-
Java
-
Kotlin
@DirtiesContext(methodMode = BEFORE_METHOD) (1) @Test void testProcessWhichRequiresFreshAppCtx() { // some logic that requires a new Spring container }
1 在目前的測試方法之前弄髒 context。 @DirtiesContext(methodMode = BEFORE_METHOD) (1) @Test fun testProcessWhichRequiresFreshAppCtx() { // some logic that requires a new Spring container }
1 在目前的測試方法之前弄髒 context。 -
-
在目前的測試之後,當在方法模式設定為
AFTER_METHOD
的方法上宣告時 (即預設方法模式)。-
Java
-
Kotlin
@DirtiesContext (1) @Test void testProcessWhichDirtiesAppCtx() { // some logic that results in the Spring container being dirtied }
1 在目前的測試方法之後弄髒 context。 @DirtiesContext (1) @Test fun testProcessWhichDirtiesAppCtx() { // some logic that results in the Spring container being dirtied }
1 在目前的測試方法之後弄髒 context。 -
如果您在 context 組態為 context 階層一部分的測試中使用 @DirtiesContext
與 @ContextHierarchy
,則可以使用 hierarchyMode
旗標來控制 context 快取清除的方式。預設情況下,會使用詳盡的演算法來清除 context 快取,不僅包括目前層級,還包括所有其他與目前測試共用共同祖先 context 的 context 階層。所有位於共同祖先 context 子階層中的 ApplicationContext
實例都會從 context 快取中移除並關閉。如果詳盡的演算法對於特定用例而言過於繁瑣,您可以指定較簡單的目前層級演算法,如下列範例所示。
-
Java
-
Kotlin
@ContextHierarchy({
@ContextConfiguration("/parent-config.xml"),
@ContextConfiguration("/child-config.xml")
})
class BaseTests {
// class body...
}
class ExtendedTests extends BaseTests {
@Test
@DirtiesContext(hierarchyMode = CURRENT_LEVEL) (1)
void test() {
// some logic that results in the child context being dirtied
}
}
1 | 使用目前層級演算法。 |
@ContextHierarchy(
ContextConfiguration("/parent-config.xml"),
ContextConfiguration("/child-config.xml"))
open class BaseTests {
// class body...
}
class ExtendedTests : BaseTests() {
@Test
@DirtiesContext(hierarchyMode = CURRENT_LEVEL) (1)
fun test() {
// some logic that results in the child context being dirtied
}
}
1 | 使用目前層級演算法。 |
如需有關 EXHAUSTIVE
與 CURRENT_LEVEL
演算法的更多詳細資訊,請參閱 DirtiesContext.HierarchyMode
javadoc。