In the Spring TestContext Framework, an `ApplicationContext` loaded for an integration test is cached and reused across subsequent tests in the same test suite run. What does the framework use as the *key* for that cache?
A. The fully qualified name of the test class that first triggered the context load.
Assumes caching is per test class, which would make the cache useless — two different test classes declaring the identical configuration would each load their own context. The key is derived from the configuration, not from the test class identity, which is precisely why unrelated classes with the same configuration share one context.
B. The unique combination of the configuration parameters used to load the context — such as the declared locations/classes, active profiles, context initializers, property sources, and the ContextLoader used.Correct answer
Correct per the TestContext framework's context caching section: the key is built from the full set of configuration attributes (locations, classes, initializers, active profiles, property sources, context customizers, context loader, parent context), so any test class producing the same key reuses the cached context.
C. The name of the JUnit test method being executed, so each test method gets its own cache entry.
Confuses the context cache with per-method test instance/transaction lifecycle. Context loading is never keyed per test method; a fresh context per method would defeat the entire purpose of caching, which exists because loading a context is expensive.
D. The bean definition count of the loaded context, so contexts with the same number of beans are treated as identical.
Invents a structural heuristic based on the loaded result rather than the declared configuration. The key must be computable *before* loading — otherwise the context would have to be built to discover it was already cached — and bean counts would collide across unrelated configurations.
Explanation
Loading an ApplicationContext is expensive, so the TestContext framework caches each loaded context in a static cache keyed by the unique combination of configuration parameters that produced it — declared resource locations and component classes, active profiles, ApplicationContextInitializers, test property sources, context customizers, the ContextLoader, and any parent context. Any test class whose configuration yields the same key transparently reuses the cached instance, which is why caching is a property of the configuration rather than of the test class, the test method, or the shape of the resulting bean factory. Keying on the test class would prevent sharing between classes with identical configuration; keying per method would eliminate the benefit entirely; and keying on a post-load property such as bean count is impossible because the key must be known before the context is loaded.