Spring Test Context Caching
Fast recall
Quick revision
Core answer
Awareness of Spring's test performance optimization and what configuration differences invalidate the cache
Using @DirtiesContext broadly or varying test configuration unnecessarily, which reduces context-cache reuse
Explain exactly what makes two contexts equivalent: same annotations, same properties, same active profiles, same classes parameter
Interview-ready guide
Interview answer
Spring's TestContext framework caches an application context so another test with the same cache key can reuse it in the same process. The key includes configuration classes and locations, profiles, test properties, initializers, customizers, and any parent context. A different key means a different context; @DirtiesContext removes a context so it must be built again when next requested. The cache is bounded and is not shared across forked processes. I keep test configuration consistent when the tests need the same context, but I do not change behavior merely to chase a cache hit. For database isolation, a test-managed transaction can roll back work done inside its boundary; it is not a general replacement for cleanup or @DirtiesContext.
Spring Test Context Caching
Direct Answer
The Spring TestContext framework caches an ApplicationContext under a key built from its configuration, including locations, classes, initializers, profiles, properties, customizers, and parent context. Tests with the same key can reuse it in the same process, avoiding another startup. The cache has a size limit, can evict entries, and is not shared across forked JVM processes.
Code Example
// Test 1 — loads a context, cached under key: default config, no properties
@SpringBootTest
class OrderServiceTest {
@Test void shouldCreateOrder() { /* uses the cached context */ }
}
// Test 2 — reuses the cached context because config is identical
@SpringBootTest
class CustomerServiceTest {
@Test void shouldCreateCustomer() { /* reuses OrderServiceTest's context */ }
}
// Test 3 — NEW context, because the properties differ
@SpringBootTest(properties = "spring.datasource.url=jdbc:h2:mem:testdb2")
class InventoryServiceTest {
@Test void shouldUpdateInventory() { /* separate context, cached separately */ }
}
// Test 4 — NEW context, because @DirtiesContext throws the old one away
@SpringBootTest
@DirtiesContext // forces a context reload after this class
class PaymentServiceTest {
@Test void shouldProcessPayment() { /* context discarded after this class */ }
}
Common Mistakes & Pitfalls
- Using @DirtiesContext everywhere: It removes a reusable context from the cache, so later matching tests must create it again.
- Varying configuration accidentally: Different properties, profiles, dynamic-property values, imports, or context-customizing mocks can produce different keys.
- Treating a context reset as database cleanup: @DirtiesContext controls the application context. For database state, use a test-managed transaction when its boundary fits, or perform explicit cleanup.
Check your understanding
Answer these without looking back at the article.
- What is @DirtiesContext and when is it actually necessary?
- How would you check how many contexts are being created during your test suite?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.