Annotation Interface DirtiesContext
ApplicationContext
associated with a test is dirty and should therefore be closed
and removed from the context cache.
Use this annotation if a test has modified the context — for example, by modifying the state of a singleton bean, modifying the state of an embedded database, etc. Subsequent tests that request the same context will be supplied a new context.
@DirtiesContext
may be used as a class-level and method-level
annotation within the same test class or test class hierarchy. In such scenarios,
the ApplicationContext
will be 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 test phases 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.
Supported Test Phases
- Before current test class: when declared at the class
level with class mode set to
BEFORE_CLASS
- Before each test method in current test class: when
declared at the class level with class mode set to
BEFORE_EACH_TEST_METHOD
- Before current test method: when declared at the
method level with method mode set to
BEFORE_METHOD
- After current test method: when declared at the
method level with method mode set to
AFTER_METHOD
- After each test method in current test class: when
declared at the class level with class mode set to
AFTER_EACH_TEST_METHOD
- After current test class: when declared at the
class level with class mode set to
AFTER_CLASS
BEFORE_*
modes are supported by the
DirtiesContextBeforeModesTestExecutionListener
; AFTER_*
modes are supported by the
DirtiesContextTestExecutionListener
.
This annotation may be used as a meta-annotation to create custom composed annotations.
As of Spring Framework 5.3, this annotation will be inherited from an
enclosing test class by default. See
@NestedTestConfiguration
for details.
- Since:
- 2.0
- Author:
- Sam Brannen, Rod Johnson
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Defines modes which determine how@DirtiesContext
is interpreted when used to annotate a test class.static enum
Defines modes which determine how the context cache is cleared when@DirtiesContext
is used in a test whose context is configured as part of a hierarchy via@ContextHierarchy
.static enum
Defines modes which determine how@DirtiesContext
is interpreted when used to annotate a test method. -
Optional Element Summary
Modifier and TypeOptional ElementDescriptionThe mode to use when a test class is annotated with@DirtiesContext
.The context cache clearing mode to use when a context is configured as part of a hierarchy via@ContextHierarchy
.The mode to use when a test method is annotated with@DirtiesContext
.
-
Element Details
-
methodMode
DirtiesContext.MethodMode methodModeThe mode to use when a test method is annotated with@DirtiesContext
.Defaults to
AFTER_METHOD
.Setting the method mode on an annotated test class has no meaning. For class-level control, use
classMode()
instead.- Since:
- 4.2
- Default:
- AFTER_METHOD
-
classMode
DirtiesContext.ClassMode classModeThe mode to use when a test class is annotated with@DirtiesContext
.Defaults to
AFTER_CLASS
.Setting the class mode on an annotated test method has no meaning. For method-level control, use
methodMode()
instead.- Since:
- 3.0
- Default:
- AFTER_CLASS
-
hierarchyMode
DirtiesContext.HierarchyMode hierarchyModeThe context cache clearing mode to use when a context is configured as part of a hierarchy via@ContextHierarchy
.Defaults to
EXHAUSTIVE
.- Since:
- 3.2.2
- Default:
- EXHAUSTIVE
-