@DirtiesContext

@DirtiesContext 表示在測試執行期間底層的 Spring ApplicationContext 已被汙染(即,測試以某種方式修改或破壞了它 — 例如,透過更改單例 Bean 的狀態),並且應該被關閉。當一個應用程式上下文被標記為髒時,它將從測試框架的快取中移除並關閉。因此,對於任何需要具有相同配置元資料的上下文的後續測試,底層的 Spring 容器將被重建。

你可以在同一個測試類或測試類層次結構中將 @DirtiesContext 用作類級別和方法級別的註解。在這種情況下,ApplicationContext 將在任何此類帶註解的方法之前或之後,以及在當前測試類之前或之後被標記為髒,具體取決於配置的 methodModeclassMode。當 @DirtiesContext 在類級別和方法級別都宣告時,將遵循兩個註解中配置的模式。例如,如果類模式設定為 BEFORE_EACH_TEST_METHOD,方法模式設定為 AFTER_METHOD,則上下文將在給定測試方法之前和之後都被標記為髒。

以下示例解釋了在各種配置場景下上下文何時會被汙染:

  • 在當前測試類之前,當在類上宣告,且類模式設定為 BEFORE_CLASS 時。

    • Java

    • Kotlin

    @DirtiesContext(classMode = BEFORE_CLASS) (1)
    class FreshContextTests {
    	// some tests that require a new Spring container
    }
    1 在當前測試類之前汙染上下文。
    @DirtiesContext(classMode = BEFORE_CLASS) (1)
    class FreshContextTests {
    	// some tests that require a new Spring container
    }
    1 在當前測試類之前汙染上下文。
  • 在當前測試類之後,當在類上宣告,且類模式設定為 AFTER_CLASS 時(即預設的類模式)。

    • Java

    • Kotlin

    @DirtiesContext (1)
    class ContextDirtyingTests {
    	// some tests that result in the Spring container being dirtied
    }
    1 在當前測試類之後汙染上下文。
    @DirtiesContext (1)
    class ContextDirtyingTests {
    	// some tests that result in the Spring container being dirtied
    }
    1 在當前測試類之後汙染上下文。
  • 在當前測試類中的每個測試方法之前,當在類上宣告,且類模式設定為 BEFORE_EACH_TEST_METHOD 時。

    • Java

    • Kotlin

    @DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD) (1)
    class FreshContextTests {
    	// some tests that require a new Spring container
    }
    1 在每個測試方法之前汙染上下文。
    @DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD) (1)
    class FreshContextTests {
    	// some tests that require a new Spring container
    }
    1 在每個測試方法之前汙染上下文。
  • 在當前測試類中的每個測試方法之後,當在類上宣告,且類模式設定為 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 在每個測試方法之後汙染上下文。
    @DirtiesContext(classMode = AFTER_EACH_TEST_METHOD) (1)
    class ContextDirtyingTests {
    	// some tests that result in the Spring container being dirtied
    }
    1 在每個測試方法之後汙染上下文。
  • 在當前測試之前,當在方法上宣告,且方法模式設定為 BEFORE_METHOD 時。

    • Java

    • Kotlin

    @DirtiesContext(methodMode = BEFORE_METHOD) (1)
    @Test
    void testProcessWhichRequiresFreshAppCtx() {
    	// some logic that requires a new Spring container
    }
    1 在當前測試方法之前汙染上下文。
    @DirtiesContext(methodMode = BEFORE_METHOD) (1)
    @Test
    fun testProcessWhichRequiresFreshAppCtx() {
    	// some logic that requires a new Spring container
    }
    1 在當前測試方法之前汙染上下文。
  • 在當前測試之後,當在方法上宣告,且方法模式設定為 AFTER_METHOD 時(即預設的方法模式)。

    • Java

    • Kotlin

    @DirtiesContext (1)
    @Test
    void testProcessWhichDirtiesAppCtx() {
    	// some logic that results in the Spring container being dirtied
    }
    1 在當前測試方法之後汙染上下文。
    @DirtiesContext (1)
    @Test
    fun testProcessWhichDirtiesAppCtx() {
    	// some logic that results in the Spring container being dirtied
    }
    1 在當前測試方法之後汙染上下文。

如果你在測試中使用 @DirtiesContext,並且該測試的上下文是使用 @ContextHierarchy 配置為上下文層次結構的一部分,你可以使用 hierarchyMode 標誌來控制上下文快取的清除方式。預設情況下,使用一種詳盡的演算法來清除上下文快取,不僅包括當前級別,還包括與當前測試共享共同祖先上下文的所有其他上下文層次結構。所有位於共同祖先上下文的子層次結構中的 ApplicationContext 例項都將從上下文快取中移除並關閉。如果詳盡的演算法對於特定用例來說過於繁瑣,你可以指定更簡單的當前級別演算法,如下例所示。

  • 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 使用當前級別演算法。

有關 EXHAUSTIVECURRENT_LEVEL 演算法的更多詳細資訊,請參閱 DirtiesContext.HierarchyMode javadoc。

© . This site is unofficial and not affiliated with VMware.