Skip to content
Veltoryx Technologies
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/42 return HTTP 200 OK?
  • Does submitting invalid JSON trigger our @Valid Bean Validation and return 400 Bad Request?
  • Does our custom @RestControllerAdvice format 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.

MockMvc Request Simulation Flow
Fig. 1 · MockMvc Request Simulation Flow

Loading the rest of the lesson…