此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10! |
@DirtiesContext
@DirtiesContext
表示底层 SpringApplicationContext
已经
在执行测试期间被弄脏(即测试在
某种方式——例如,通过更改单例 Bean 的状态)并且应该是
闭。当应用程序上下文被标记为脏时,它将从测试中删除
框架的缓存并关闭。因此,底层 Spring 容器是
为需要具有相同配置的上下文的任何后续测试重新构建
元数据。
您可以使用@DirtiesContext
作为类级和方法级注释
相同的测试类或测试类层次结构。在这种情况下,ApplicationContext
在任何此类注释方法之前或之后以及之前或之后都标记为脏
当前测试类,具体取决于配置的methodMode
和classMode
.什么时候@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 在当前测试类之后弄脏上下文。 -
-
在当前测试类中的每个测试方法之前,当在类mode 设置为
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 在每个测试方法之前弄脏上下文。 -
-
在当前测试类中的每个测试方法之后,当在类mode 设置为
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
驻留在共同祖先的子层次结构中的实例context 将从上下文缓存中删除并关闭。如果穷举算法是overkill 对于特定用例,您可以指定更简单的当前级别算法如以下示例所示。
-
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 的文档。