diff --git a/spring-framework-reference/src/testing.xml b/spring-framework-reference/src/testing.xml index a08944853f..9dbe84d626 100644 --- a/spring-framework-reference/src/testing.xml +++ b/spring-framework-reference/src/testing.xml @@ -1606,16 +1606,86 @@ public class TransferServiceTest {
Context caching - By default, once an - ApplicationContext has been loaded - for a test it will be reused for Once the TestContext framework loads an + ApplicationContext for a test, that + context will be cached and reused for all subsequent tests that declare the same - unique context configuration within the same - process — for example, all tests run in a suite within an IDE or all - tests run for the same project with a build framework like Ant or - Maven. Thus the setup cost for loading an application context is - incurred only once (per test suite), and subsequent test execution - is much faster. + unique context configuration within the same test suite. To + understand how caching works, it is important to understand what is + meant by unique and test + suite. + + An ApplicationContext can be + uniquely identified by the combination of + configuration parameters that are used to load it. Consequently, the + unique combination of configuration parameters are used to generate + a key under which the context is cached. The + TestContext framework uses the following configuration parameters to + build the context cache key: + + + + locations (from + @ContextConfiguration) + + + + classes (from + @ContextConfiguration) + + + + contextLoader (from + @ContextConfiguration) + + + + activeProfiles (from + @ActiveProfiles) + + + + For example, if TestClassA specifies + {"app-config.xml", "test-config.xml"} for the + locations (or value) attribute + of @ContextConfiguration, the + TestContext framework will load the corresponding + ApplicationContext and store it in a + static context 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) and does not define a different + ContextLoader or different active + profiles, then the same + ApplicationContext will be 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. + + + Test suites and forked processes + + The Spring TestContext framework stores application contexts + in a static cache. This means that the + context is literally stored in a static + variable. In other words, if tests execute in separate processes + the static cache will be cleared between each test execution, and + this will effectively disable the caching mechanism. + + To benefit from the caching mechanism, all tests must run + within the same process or test suite. This can be achieved by + executing all tests as a group within an IDE. Similarly, when + executing tests with a build framework such as Ant or Maven it is + important to make sure that the build framework does not + fork between tests. For example, if the + forkMode + for the Maven Surefire plug-in is set to always + or pertest, the TestContext framework will not + be able to cache application contexts between test classes and the + build process will run significantly slower as a result. + In the unlikely case that a test corrupts the application context and requires reloading — for example, by modifying a bean @@ -1624,10 +1694,10 @@ public class TransferServiceTest { @DirtiesContext (see the discussion of @DirtiesContext in ). This instructs - Spring to reload the configuration and rebuild the application - context before executing the next test. Note that support for the - @DirtiesContext annotation is - provided by the + Spring to remove the context from the cache and rebuild the + application context before executing the next test. Note that + support for the @DirtiesContext + annotation is provided by the DirtiesContextTestExecutionListener which is enabled by default.