@DirtiesContext in Spring

@DirtiesContext is a test annotation which indicates that the  ApplicationContext associated with a test is dirty and should therefore be closed and removed from the context cache.

Once the TestContext framework loads an  ApplicationContext  (or WebApplicationContext) for a test, that context is cached and reused for all subsequent tests that declare the same unique context configuration within the same test suite.

For example, if TestClassA specifies {"app-config.xml", "test-config.xml"} for the locations (or value) attribute of @ContextConfiguration, the TestContext framework loads the corresponding ApplicationContext and stores it in a staticcontext cache under a key that is based solely on those locations. So, if TestClassB also defines {"app-config.xml", "test-config.xml"} for its locations (either explicitly or implicitly through inheritance) but does not define @WebAppConfiguration, a different ContextLoader, different active profiles, different context initializers, different test property sources, or a different parent context, but the same ApplicationContext is shared by both test classes. This means that the setup cost for loading an application context is incurred only once (per test suite), and subsequent test execution is much faster.

It is very unlikely that a test corrupts the application context and requires reloading (for example, by modifying a bean definition or the state of an application object), you can annotate your test class or test method with @DirtiesContext. This instructs Spring to remove the context from the cache and rebuild the application context before running the next test that requires the same application context.

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

You can use @DirtiesContext as both a class-level and a method-level annotation within the same class or 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.

  • The following examples explain when the context would be dirtied for various configuration scenarios
@DirtiesContext(classMode = BEFORE_CLASS) 
public class FreshContextTests {
    // some tests that require a new Spring container
}
  • After the current test class, when declared on a class with class mode set to AFTER_CLASS (i.e., the default class mode).
@DirtiesContext 
public class ContextDirtyingTests {
    // some tests that result in the Spring container being dirtied
}
  • Before each test method in the current test class, when declared on a class with class mode set to BEFORE_EACH_TEST_METHOD.
@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD) 
public class FreshContextTests {
    // some tests that require a new Spring container
}
  • After each test method in the current test class, when declared on a class with class mode set to AFTER_EACH_TEST_METHOD.
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD) 
public class ContextDirtyingTests {
    // some tests that result in the Spring container being dirtied
}
  • Before the current test, when declared on a method with the method mode set to BEFORE_METHOD.
@DirtiesContext(methodMode = BEFORE_METHOD) 
@Test
public void testProcessWhichRequiresFreshAppCtx() {
    // some logic that requires a new Spring container
}
  • After the current test, when declared on a method with the method mode set to AFTER_METHOD (i.e., the default method mode).
@DirtiesContext 
@Test
public void testProcessWhichDirtiesAppCtx() {
    // some logic that results in the Spring container being dirtied
}

Hope this have cleared some of your doubts in @DirtiesContext