@SpringBootTest and Its Slice Variants
Fast recall
Quick revision
Core answer
Whether you understand Spring Boot's test slices and the cost of full context loading
Using @SpringBootTest for every test — it is slow and causes context reloading; slices are much faster and more focused
Explain that @WebMvcTest does NOT load the service layer, so you must mock services with @MockBean, and why that separation is intentional
Interview-ready guide
Interview answer
@SpringBootTest loads the full application context — every bean, every auto-configuration. It gives maximum fidelity but takes 10-30 seconds to start. Test slices like @WebMvcTest and @DataJpaTest load only the relevant portion, starting in under 2 seconds.
@WebMvcTest(UserController.class) loads just the controller layer — no services, no repositories, no database. You mock dependencies with @MockBean. @DataJpaTest loads JPA repositories with an embedded database — no controllers.
The rule: use the narrowest slice that covers what you're testing. Reserve @SpringBootTest for smoke tests and cross-cutting integration tests — maybe 10-20% of your suite. The rest should use slices.
One thing that trips people up: Spring caches contexts, but each unique @MockBean combination creates a new cache key. Minimizing distinct mock combinations across test classes keeps caching effective and the suite fast.
@SpringBootTest and Its Slice Variants
Direct Answer
@SpringBootTest loads the full application context and is great for true integration tests, but is slow. Slice annotations load only the relevant portion: @WebMvcTest loads only the web layer (controllers, filters, security), @DataJpaTest loads only the persistence layer (repositories, entities, DataSource). Use slices to get faster, more focused tests without bootstrapping the entire context.
Common Mistakes & Pitfalls
Using @SpringBootTest for every test — it is slow and causes context reloading; slices are much faster and more focused
Check your understanding
Answer these without looking back at the article.
- How does Spring Boot's application context caching work in tests?
- When would you add @AutoConfigureMockMvc to @SpringBootTest instead of using @WebMvcTest?
- How do you test a @ControllerAdvice exception handler?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.