spring-framework / org.springframework.test.annotation / DirtiesContext

DirtiesContext

@Target([AnnotationTarget.CLASS, AnnotationTarget.FILE, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER]) @Inherited class DirtiesContext

Test annotation which indicates that the org.springframework.context.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.

Supported Test Phases

BEFORE_* modes are supported by the org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener; AFTER_* modes are supported by the org.springframework.test.context.support.DirtiesContextTestExecutionListener.

Author
Sam Brannen

Author
Rod Johnson

Since
2.0

See Also
org.springframework.test.context.ContextConfigurationorg.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListenerorg.springframework.test.context.support.DirtiesContextTestExecutionListener

Constructors

<init>

DirtiesContext(methodMode: MethodMode, classMode: ClassMode, hierarchyMode: HierarchyMode)

Test annotation which indicates that the org.springframework.context.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.

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 org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener; AFTER_* modes are supported by the org.springframework.test.context.support.DirtiesContextTestExecutionListener.

Properties

classMode

val classMode: ClassMode

The 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.

hierarchyMode

val hierarchyMode: HierarchyMode

The context cache clearing mode to use when a context is configured as part of a hierarchy via org.springframework.test.context.ContextHierarchy.

Defaults to EXHAUSTIVE.

methodMode

val methodMode: MethodMode

The 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.