ProLesson 7.2Intermediate2 min readUpdated 31 October 2026
Web Layer Testing with @WebMvcTest and MockMvc
Test REST controllers in isolation with MockMvc, verify JSON responses with JsonPath, and mock dependencies with @MockBean.
01Testing the web layer without a web server
When testing an HTTP controller, you want to verify:
- Does
GET /api/v1/orders/42return HTTP200 OK? - Does submitting invalid JSON trigger our
@ValidBean Validation and return400 Bad Request? - Does our custom
@RestControllerAdviceformat the error response properly?
Starting a real Tomcat server and opening TCP network ports for every test is slow and resource-heavy.
Spring Boot provides @WebMvcTest: a lightweight slice test that boots only the web layer (Controllers, Converters, Filters, and ControllerAdvice), while leaving all database and service beans uninstantiated.
Loading the rest of the lesson…