Skip to content
Latchkey

Spring Boot slow context: too many @SpringBootTest instead of slices in CI

When every test loads a full @SpringBootTest context, CI spends most of its time building and rebuilding contexts. Test slices load only the beans a layer needs, so they start faster and reuse a cached context.

What this error means

The Spring Boot test job is slow and sometimes times out or runs out of memory in CI; logs show the full context being built repeatedly for controller or repository tests.

Spring Boot
Started ExampleApplication in 6.42 seconds (process running for 7.1)
... (repeated for dozens of @SpringBootTest classes) ...
Error: The operation was canceled.  # job timeout

Common causes

Every test loads the whole application

Full-context tests bootstrap the entire app (web server, JPA, all beans) even to test one controller, multiplying startup cost.

Varied configurations defeat context caching

Different properties or mocks per test create many distinct contexts, so caching cannot help.

How to fix it

Use a controller slice for web tests

Load only the MVC layer with @WebMvcTest and mock the services below it.

src/test/java
@WebMvcTest(OrderController.class)
class OrderControllerTest {
  @Autowired MockMvc mvc;
  @MockBean OrderService orderService;
}

Use a persistence slice for repositories

Load only JPA with @DataJpaTest so repository tests skip the web layer entirely.

src/test/java
@DataJpaTest
class OrderRepositoryTest {
  @Autowired OrderRepository repo;
}

How to prevent it

  • Reserve @SpringBootTest for genuine end-to-end tests.
  • Keep test configurations uniform so the context cache is effective.
  • Use @WebMvcTest/@DataJpaTest slices for layer-focused tests.

Frequently asked questions

What causes "Slow full-context tests in CI"?
Full-context tests bootstrap the entire app (web server, JPA, all beans) even to test one controller, multiplying startup cost.
How do I fix Slow full-context tests in CI?
Load only the MVC layer with @WebMvcTest and mock the services below it.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card