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.
Started ExampleApplication in 6.42 seconds (process running for 7.1)
... (repeated for dozens of @SpringBootTest classes) ...
Error: The operation was canceled. # job timeoutCommon 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.
@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.
@DataJpaTest
class OrderRepositoryTest {
@Autowired OrderRepository repo;
}How to prevent it
- Reserve
@SpringBootTestfor genuine end-to-end tests. - Keep test configurations uniform so the context cache is effective.
- Use
@WebMvcTest/@DataJpaTestslices for layer-focused tests.