Testing Web Layers with @WebMvcTest and MockMvc
Slice-test Spring Boot controllers in isolation using @WebMvcTest and MockMvc without starting the full application context.
Testing Web Layers with @WebMvcTest and MockMvc is a free Testing Mastery: JUnit, Mockito & Integration Tests lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Testing Mastery: JUnit, Mockito & Integration Tests learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Slice Tests?
Loading the entire Spring context for every controller test is slow. Spring Boot offers test slices that load only the beans you need. @WebMvcTest loads just the web layer.
What @WebMvcTest Loads
@WebMvcTest configures controllers, filters, and JSON converters, but not services or repositories. Those collaborators are supplied as mocks.
Declaring the Test
Target a single controller so the slice stays small and fast.
@WebMvcTest(UserController.class)
class UserControllerTest {
@Autowired MockMvc mockMvc;
}Mocking the Service
The controller's service dependency is replaced with a Mockito mock using @MockBean, which places the mock in the context.
@MockBean
UserService userService;Performing a Request
MockMvc simulates HTTP requests against your controller without a running server.
mockMvc.perform(get("/users/1"))
.andExpect(status().isOk());Stubbing Then Asserting
Stub the mocked service, fire the request, and assert on the response body and status.
when(userService.find(1L))
.thenReturn(new User(1L, "Ada"));
mockMvc.perform(get("/users/1"))
.andExpect(jsonPath("$.name").value("Ada"));Checking Status Codes
Validate error mappings, such as a 404 when the service signals a missing entity.
when(userService.find(9L))
.thenThrow(new NotFoundException());
mockMvc.perform(get("/users/9"))
.andExpect(status().isNotFound());Testing POST with a Body
Send JSON content and verify the controller deserializes and responds correctly.
mockMvc.perform(post("/users")
.contentType("application/json")
.content("{\"name\":\"Ada\"}"))
.andExpect(status().isCreated());JsonPath Assertions
jsonPath expressions let you assert on nested fields, array sizes, and values inside the JSON response.
.andExpect(jsonPath("$.items.length()").value(3))Slice vs Full Context
Use @WebMvcTest for focused, fast web-layer tests. Reserve @SpringBootTest for true end-to-end flows where you want the whole context wired.
Speed Benefits
Because only the web layer loads, these tests start in a fraction of the time of a full-context test, encouraging more thorough controller coverage.
Quick Check
How are service dependencies handled in a @WebMvcTest?
Recap
You learned web-layer slice testing:
@WebMvcTestloads only controllers and web infrastructureMockMvcsimulates HTTP requests without a server@MockBeansupplies mocked services- Assert status and body with
jsonPath
Frequently asked questions
Is the “Testing Web Layers with @WebMvcTest and MockMvc” lesson free?
Yes — the full text of “Testing Web Layers with @WebMvcTest and MockMvc” is free to read here on the web, and the Testing Mastery: JUnit, Mockito & Integration Tests course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Testing Mastery: JUnit, Mockito & Integration Tests course, upgrade to CoddyKit PRO.
What will I learn in “Testing Web Layers with @WebMvcTest and MockMvc”?
Slice-test Spring Boot controllers in isolation using @WebMvcTest and MockMvc without starting the full application context. You practise Testing Mastery: JUnit, Mockito & Integration Tests with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Testing Mastery: JUnit, Mockito & Integration Tests?
No prior experience is required. Testing Mastery: JUnit, Mockito & Integration Tests on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Testing Web Layers with @WebMvcTest and MockMvc” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Testing Mastery: JUnit, Mockito & Integration Tests lesson?
Yes. Every Testing Mastery: JUnit, Mockito & Integration Tests lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Spring Test Context Framework
- Testing RESTful APIs
- Embedded Databases for Tests
- Testing Web Layers with @WebMvcTest and MockMvc