Spring: @SpringBootTest is heavier than you usually need

Slice annotations start only the layer under test, so the suite stays fast.

Code
@WebMvcTest(OrderController.class)   // web layer only, service is mocked
class OrderControllerTest {
    @Autowired MockMvc mvc;
    @MockitoBean OrderService service;
}

@DataJpaTest                          // JPA + in-memory DB only
class OrderRepositoryTest {
    @Autowired OrderRepository repo;
}

@SpringBootTest                       // the WHOLE application context
class FullIntegrationTest { }
Output
@WebMvcTest: ~0.4s per class
@SpringBootTest: seconds, and one context per distinct configuration
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11