@SpringBootTest vs Slice Tests Spring Boot | Java Fresher
Fast recall
Quick revision
Core answer
@SpringBootTest builds a broad application context from the application's Spring Boot configuration. Its default webEnvironment is MOCK, so it does not start a live server unless you choose RANDOM_PORT or DEFINED_PORT. A slice such as @WebMvcTest or @DataJpaTest limits configuration to one layer. Pick the narrowest context that proves the behavior.Interview-ready guide
Interview answer
@SpringBootTest creates a broad Spring Boot application context, so it is useful for checking bean wiring, configuration, and behavior that crosses layers.
It does not automatically make the test end to end: its default web environment is MOCK, which creates a web context without a live server. I use RANDOM_PORT when I need to send HTTP requests through a real server. Slice tests are narrower.
@WebMvcTest focuses on MVC behavior, while @DataJpaTest focuses on entities and repositories. A slice will not discover every service in the application, so needed collaborators must be imported or replaced. I choose the smallest test context that contains the behavior: a plain unit test for service logic, a slice for one framework layer, and a broad context only for integration across those boundaries.
@SpringBootTest vs Slice Tests Spring Boot | Java Fresher
Direct Answer
@SpringBootTest builds a broad application context from the application's Spring Boot configuration. Its default webEnvironment is MOCK, so it does not start a live server unless you choose RANDOM_PORT or DEFINED_PORT. A slice such as @WebMvcTest or @DataJpaTest limits configuration to one layer. Pick the narrowest context that proves the behavior.
Code Example
// Broad context, but no live server with the default MOCK web mode
@SpringBootTest
class ApplicationWiringTest { }
// MVC slice with a context-level service replacement
@WebMvcTest(OrderController.class)
class OrderControllerTest {
@Autowired MockMvc mvc;
@MockitoBean OrderService service;
}
// Broad context plus a live server
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class HttpIntegrationTest { }
Common Mistakes & Pitfalls
- Calling the default an end-to-end test:
@SpringBootTestuses a mock web environment unless configured otherwise. - Assuming a slice has all application beans: slices intentionally restrict scanning and auto-configuration.
- Using the broadest context for simple logic: a direct unit test is clearer when Spring behavior is irrelevant.
- Mocking the feature under test: a repository query test needs the repository and database, not a mocked repository.
- Ignoring context reuse: inconsistent properties and bean overrides can create different cache keys.
Check your understanding
Answer these without looking back at the article.
- What does context caching mean in Spring Boot tests?
- How does @WebMvcTest interact with security configuration?
- How do you test with a real database instead of H2?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.