@DirtiesContext

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

你可以在同一個測試類或測試類層級結構中將 @DirtiesContext 用作類級別和方法級別的註解。在這種情況下,根據配置的 methodModeclassModeApplicationContext 會在任何此類帶註解的方法之前或之後,以及在當前測試類之前或之後被標記為“髒”。當 @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。