@Documented @Inherited @Retention(value=RUNTIME) @Target(value={TYPE,METHOD}) public @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 class or 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()
.
As of Spring Framework 4.0, this annotation may be used as a meta-annotation to create custom composed annotations.
BEFORE_CLASS
BEFORE_EACH_TEST_METHOD
BEFORE_METHOD
AFTER_METHOD
AFTER_EACH_TEST_METHOD
AFTER_CLASS
BEFORE_*
modes are supported by the
DirtiesContextBeforeModesTestExecutionListener
;
AFTER_*
modes are supported by the
DirtiesContextTestExecutionListener
.
ContextConfiguration
,
DirtiesContextBeforeModesTestExecutionListener
,
DirtiesContextTestExecutionListener
Modifier and Type | Optional Element and Description |
---|---|
DirtiesContext.ClassMode |
classMode
The mode to use when a test class is annotated with
@DirtiesContext . |
DirtiesContext.HierarchyMode |
hierarchyMode
The context cache clearing mode to use when a context is
configured as part of a hierarchy via
@ContextHierarchy . |
DirtiesContext.MethodMode |
methodMode
The mode to use when a test method is annotated with
@DirtiesContext . |
public abstract DirtiesContext.MethodMode methodMode
@DirtiesContext
.
Defaults to AFTER_METHOD
.
Setting the method mode on an annotated test class has no meaning.
For class-level control, use classMode()
instead.
public abstract DirtiesContext.ClassMode classMode
@DirtiesContext
.
Defaults to AFTER_CLASS
.
Setting the class mode on an annotated test method has no meaning.
For method-level control, use methodMode()
instead.
public abstract DirtiesContext.HierarchyMode hierarchyMode
@ContextHierarchy
.
Defaults to EXHAUSTIVE
.