@Documented
@Inherited
@Retention(value=RUNTIME)
@Target(value={TYPE,METHOD})
public @interface DirtiesContext
ApplicationContext
associated with a test is dirty and should be closed:
AFTER_EACH_TEST_METHOD
AFTER_CLASS
Use this annotation if a test has modified the context — for example, by replacing a bean definition or changing the state of a singleton bean. Subsequent tests will be supplied a new context.
@DirtiesContext
may be used as a class-level and method-level
annotation within the same class. In such scenarios, the
ApplicationContext
will be marked as dirty after any
such annotated method as well as after the entire class. If the
DirtiesContext.ClassMode
is set to AFTER_EACH_TEST_METHOD
, the context will be marked dirty after each test
method in the class.
As of Spring Framework 4.0, this annotation may be used as a meta-annotation to create custom composed annotations.
ContextConfiguration
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 . |
public abstract DirtiesContext.ClassMode classMode
@DirtiesContext
.
Defaults to AFTER_CLASS
.
Note: Setting the class mode on an annotated test method has no meaning,
since the mere presence of the @DirtiesContext
annotation on a
test method is sufficient.
public abstract DirtiesContext.HierarchyMode hierarchyMode
@ContextHierarchy
.
Defaults to EXHAUSTIVE
.