Skip to content
Preptima
mediumScenarioConceptMidSeniorStaff

The test suite has 400 @SpringBootTest classes and takes twenty minutes. Where does the time go?

Mostly into starting Spring application contexts. Context caching reuses one context per distinct test configuration, so each unique mock bean, property set, profile or @DirtiesContext forces a fresh start. Count contexts from the cache statistics log, converge the configurations, and move narrow tests to slices.

5 min readUpdated 2026-10-09Target archetype: Enterprise Captive

Answer

Short answer

The time goes into starting Spring application contexts, not into running assertions. Spring context caching reuses a context across test classes only when their merged configuration is identical. Each extra mock bean, property override, profile or @DirtiesContext therefore pays for another full startup. Measure the context count from the cache statistics, converge the configurations, and move narrow tests to slices or to plain unit tests.

Was this answer helpful?

Likely follow-ups

  • How would you remove a per-class @MockitoBean without losing the stubbing that class relies on?
  • What resource problems appear when thirty cached contexts each keep a connection pool open?
  • Which tests in a suite like this should become plain unit tests with no Spring at all?
  • How does a @DynamicPropertySource method declared in each test class affect cache hits?

Related questions

Further reading