Bird
0
0

Why is @WebMvcTest preferred over @SpringBootTest for controller unit tests?

hard📝 Conceptual Q10 of 15
Spring Boot - Testing Spring Boot Applications
Why is @WebMvcTest preferred over @SpringBootTest for controller unit tests?
ABecause it loads the entire application context for full integration
BBecause it automatically mocks all dependencies
CBecause it disables all Spring features except controllers
DBecause it loads only the web layer, making tests faster and more focused
Step-by-Step Solution
Solution:
  1. Step 1: Compare @WebMvcTest and @SpringBootTest

    @WebMvcTest loads only the web layer, while @SpringBootTest loads the full application context.
  2. Step 2: Benefits of @WebMvcTest

    Loading only controllers makes tests faster, simpler, and focused on web layer behavior.
  3. Final Answer:

    Because it loads only the web layer, making tests faster and more focused -> Option D
  4. Quick Check:

    @WebMvcTest = focused, fast controller tests [OK]
Quick Trick: Use @WebMvcTest for fast, focused controller tests [OK]
Common Mistakes:
  • Thinking it loads full app context
  • Assuming it disables Spring features
  • Believing it mocks all dependencies automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes