|
对于最新的稳定版本,请使用 Spring Framework 7.0.6! |
@DirtiesContext
@DirtiesContext 表示底层的 Spring ApplicationContext 在测试执行期间已被污染(即测试以某种方式修改或破坏了它——例如,通过更改单例 bean 的状态),因此应被关闭。当应用上下文被标记为污染状态时,它将被从测试框架的缓存中移除并关闭。因此,对于任何需要相同配置元数据的后续测试,底层的 Spring 容器将被重建。
您可以在同一个类或类层次结构中将@DirtiesContext同时用作类级别和方法级别注解。在此类场景中,ApplicationContext会在任何此类带注解方法执行之前或之后,以及当前测试类执行之前或之后被标记为dirty,具体取决于配置的methodMode和classMode。当@DirtiesContext在类级别和方法级别同时声明时,两个注解的配置模式都将生效。例如,若类模式设置为BEFORE_EACH_TEST_METHOD而方法模式设置为AFTER_METHOD,则上下文将在给定测试方法执行前后均被标记为dirty。
以下示例解释了针对不同配置场景,何时会将上下文标记为脏:
-
在当前测试类运行之前,当在类上声明且类模式设置为
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 污染当前测试方法之后的应用上下文。 -
如果在上下文中配置为@ContextHierarchy的上下文层次结构的一部分的测试中使用@DirtiesContext,可以使用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 | 使用当前层级算法。 |
有关EXHAUSTIVE和CURRENT_LEVEL算法的更多详细信息,请参阅
DirtiesContext.HierarchyMode
javadoc。