0
0
Spring Bootframework~5 mins

@WebMvcTest for controller testing in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @WebMvcTest annotation in Spring Boot?

@WebMvcTest is used to test Spring MVC controllers. It loads only the web layer, not the full application context, making tests faster and focused on controller behavior.

Click to reveal answer
intermediate
Which Spring components are loaded when using @WebMvcTest?

Only the web layer components like controllers, @ControllerAdvice, Filter, and WebMvcConfigurer are loaded. Services, repositories, and other beans are not loaded unless explicitly mocked.

Click to reveal answer
intermediate
How do you mock a service dependency in a @WebMvcTest controller test?

You use @MockBean to create a mock of the service and inject it into the controller during the test.

Click to reveal answer
beginner
What is the role of MockMvc in @WebMvcTest?

MockMvc allows you to perform HTTP requests to the controller and verify responses without starting a real server.

Click to reveal answer
beginner
Why is @WebMvcTest preferred over @SpringBootTest for controller tests?

@WebMvcTest loads only the web layer, making tests faster and more focused. @SpringBootTest loads the full application context, which is slower and less focused for controller testing.

Click to reveal answer
What does @WebMvcTest load for testing?
AOnly the web layer (controllers and related components)
BThe full application context
COnly service and repository beans
DOnly database connections
Which annotation is used to mock a service in a @WebMvcTest?
A@Autowired
B@Component
C@Service
D@MockBean
What is the main tool to perform HTTP requests in @WebMvcTest?
AMockMvc
BWebClient
CRestTemplate
DHttpClient
Why might you prefer @WebMvcTest over @SpringBootTest for controller tests?
AIt loads the full application context
BIt is slower but more thorough
CIt loads only the web layer, making tests faster
DIt automatically tests the database
If your controller depends on a service, what must you do in a @WebMvcTest?
ANothing, the service is loaded automatically
BMock the service with <code>@MockBean</code>
CUse <code>@Autowired</code> on the service
DUse <code>@Service</code> on the test class
Explain how @WebMvcTest helps in testing Spring Boot controllers and what you need to do to handle service dependencies.
Think about what parts of the app are loaded and how to provide missing parts.
You got /4 concepts.
    Describe the difference between @WebMvcTest and @SpringBootTest when testing controllers.
    Consider scope and speed of tests.
    You got /4 concepts.