0Pricing
Spring Boot 4 Complete Guide · Lesson

Testing Web Controllers with MockMvc

Write focused tests for your REST endpoints using MockMvc to simulate HTTP requests without starting a real server.

Testing Web Controllers with MockMvc is a free Spring Boot 4 Complete Guide 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 Spring Boot 4 Complete Guide learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Test Controllers in Isolation?

Controller tests verify routing, request mapping, status codes, and JSON serialization without the cost of a full server. MockMvc makes this fast and precise.

Introducing @WebMvcTest

The @WebMvcTest annotation loads only the web layer, your controllers and their supporting beans, keeping the test slice small and quick.

@WebMvcTest(UserController.class)
class UserControllerTest {
}

Injecting MockMvc

Inside a @WebMvcTest, Spring provides an auto-configured MockMvc instance you autowire to perform simulated requests.

@Autowired
MockMvc mockMvc;

Performing a GET Request

Use the fluent API to perform a request and chain expectations on the response.

mockMvc.perform(get("/users/1"))
    .andExpect(status().isOk());

Mocking the Service Layer

Controllers depend on services, so you replace them with mocks using @MockBean. This isolates the controller from real business logic.

@MockBean
UserService userService;

Stubbing Behavior

Combine Mockito stubbing with the request to control what the service returns.

when(userService.find(1L)).thenReturn(new User("Ada"));

Asserting JSON Content

Use jsonPath to verify specific fields in the response body.

mockMvc.perform(get("/users/1"))
    .andExpect(jsonPath("$.name").value("Ada"));

Testing POST with a Body

Send a JSON payload and assert the created status, verifying your endpoint accepts and processes input correctly.

mockMvc.perform(post("/users")
    .contentType(MediaType.APPLICATION_JSON)
    .content("{\"name\":\"Ada\"}"))
    .andExpect(status().isCreated());

Verifying Validation Errors

Send invalid input and assert a 400 Bad Request to confirm your validation rules actually fire.

mockMvc.perform(post("/users")
    .contentType(MediaType.APPLICATION_JSON)
    .content("{}"))
    .andExpect(status().isBadRequest());

Verifying Interactions

Use Mockito's verify to confirm the controller actually called the service the expected number of times.

verify(userService).find(1L);

When to Use Full Integration Tests

MockMvc slice tests are fast but mock the service. For end-to-end confidence across the real stack, complement them with full @SpringBootTest integration tests.

Quick Check

Test your understanding of MockMvc controller testing.

Recap

You used @WebMvcTest with MockMvc to test endpoints, mocked services with @MockBean, and asserted status codes and JSON. These slice tests are a fast first line of defense.

Frequently asked questions

Is the “Testing Web Controllers with MockMvc” lesson free?

Yes — the full text of “Testing Web Controllers with MockMvc” is free to read here on the web, and the Spring Boot 4 Complete Guide 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 Spring Boot 4 Complete Guide course, upgrade to CoddyKit PRO.

What will I learn in “Testing Web Controllers with MockMvc”?

Write focused tests for your REST endpoints using MockMvc to simulate HTTP requests without starting a real server. You practise Spring Boot 4 Complete Guide 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 Spring Boot 4 Complete Guide?

No prior experience is required. Spring Boot 4 Complete Guide 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 Controllers with 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 Spring Boot 4 Complete Guide lesson?

Yes. Every Spring Boot 4 Complete Guide 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

  1. Unit Testing with JUnit & Mockito
  2. Integration Testing with Spring Boot
  3. Slice Testing & TestContainers
  4. Testing Web Controllers with MockMvc
← Back to Spring Boot 4 Complete Guide