@DirtiesContext
@DirtiesContext
表示在执行测试期间底层 Spring ApplicationContext
已被弄脏(即,测试以某种方式修改或破坏它 - 例如,通过更改单例 bean 的状态),并且应该关闭。当应用程序上下文被标记为脏时,它将从测试框架的缓存中删除并关闭。结果,为任何需要具有相同配置元数据的上下文的后续测试重建底层 Spring 容器。
@DirtiesContext
indicates that the underlying Spring ApplicationContext
has been
dirtied during the execution of a test (that is, the test modified or corrupted it in
some manner — for example, by changing the state of a singleton bean) and should be
closed. When an application context is marked as dirty, it is removed from the testing
framework’s cache and closed. As a consequence, the underlying Spring container is
rebuilt for any subsequent test that requires a context with the same configuration
metadata.
你可以在同一个测试类或测试类层次结构中将 @DirtiesContext
同时用作类级别和方法级别的注释。在这样的场景中,ApplicationContext
在任何此类带注释的方法之前或之后以及在当前测试类之前或之后被标记为脏,具体取决于配置的 methodMode
和 classMode
。当 @DirtiesContext
在类级别和方法级别同时声明时,将遵循来自这两个注释的配置模式。例如,如果类模式设置为 BEFORE_EACH_TEST_METHOD
而方法模式设置为 AFTER_METHOD
,则上下文将在给定测试方法之前和之后被标记为脏。
You can use @DirtiesContext
as both a class-level and a method-level annotation within
the same test class or test class hierarchy. In such scenarios, the ApplicationContext
is marked as dirty before or after any such annotated method as well as before or after
the current test class, depending on the configured methodMode
and classMode
. When
@DirtiesContext
is declared at both the class level and the method level, the
configured modes from both annotations will be honored. For example, if the class mode is
set to BEFORE_EACH_TEST_METHOD
and the method mode is set to AFTER_METHOD
, the
context will be marked as dirty both before and after the given test method.
以下示例说明了在各种配置场景中何时会弄脏上下文:
The following examples explain when the context would be dirtied for various configuration scenarios:
-
Before the current test class, when declared on a class with class mode set to
BEFORE_CLASS
.
- Java
-
@DirtiesContext(classMode = BEFORE_CLASS) (1) class FreshContextTests { // some tests that require a new Spring container }
1 | Dirty the context before the current test class.
|
2 | Dirty the context before the current test class. |
-
After the current test class, when declared on a class with class mode set to
AFTER_CLASS
(i.e., the default class mode).
- Java
-
@DirtiesContext (1) class ContextDirtyingTests { // some tests that result in the Spring container being dirtied }
1 | Dirty the context after the current test class.
|
2 | Dirty the context after the current test class. |
-
Before each test method in the current test class, when declared on a class with class mode set to
BEFORE_EACH_TEST_METHOD.
- Java
-
@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD) (1) class FreshContextTests { // some tests that require a new Spring container }
1 | Dirty the context before each test method.
|
2 | Dirty the context before each test method. |
-
After each test method in the current test class, when declared on a class with class mode set to
AFTER_EACH_TEST_METHOD.
- Java
-
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD) (1) class ContextDirtyingTests { // some tests that result in the Spring container being dirtied }
1 | Dirty the context after each test method.
|
2 | Dirty the context after each test method. |
-
Before the current test, when declared on a method with the method mode set to
BEFORE_METHOD
.
- Java
-
@DirtiesContext(methodMode = BEFORE_METHOD) (1) @Test void testProcessWhichRequiresFreshAppCtx() { // some logic that requires a new Spring container }
1 | Dirty the context before the current test method.
|
2 | Dirty the context before the current test method. |
-
After the current test, when declared on a method with the method mode set to
AFTER_METHOD
(i.e., the default method mode).
- Java
-
@DirtiesContext (1) @Test void testProcessWhichDirtiesAppCtx() { // some logic that results in the Spring container being dirtied }
1 | Dirty the context after the current test method.
|
2 | Dirty the context after the current test method. |
如果在 @ContextHierarchy
的 context 层次结构中配置的测试中使用 @DirtiesContext
,则可以使用 hierarchyMode
标志控制如何清除 context 缓存。默认情况下,使用穷举算法来清除 context 缓存,不仅包括当前级别,还包括与当前测试共用常见祖先 context 的所有其他 context 层次结构。所有位于常见祖先 context 的从属层次结构中的 ApplicationContext
实例都会从 context 缓存中移除并关闭。如果对于特定用例来说穷举算法太过,则可以指定更简单的当前级别算法,如下面的示例所示。
If you use @DirtiesContext
in a test whose context is configured as part of a context
hierarchy with @ContextHierarchy
, you can use the hierarchyMode
flag to control how
the context cache is cleared. By default, an exhaustive algorithm is used to clear the
context cache, including not only the current level but also all other context
hierarchies that share an ancestor context common to the current test. All
ApplicationContext
instances that reside in a sub-hierarchy of the common ancestor
context are removed from the context cache and closed. If the exhaustive algorithm is
overkill for a particular use case, you can specify the simpler current level algorithm,
as the following example shows.
- Java
-
@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 | Use the current-level algorithm.
|
2 | Use the current-level algorithm. |
有关 EXHAUSTIVE
和 CURRENT_LEVEL
算法的进一步详细信息,请参见 DirtiesContext.HierarchyMode
javadoc。
For further details regarding the EXHAUSTIVE
and CURRENT_LEVEL
algorithms, see the
DirtiesContext.HierarchyMode
javadoc.