0Pricing
Spring Boot 4 Microservices & REST APIs · Lesson

Web Layer Tests with MockMvc

Test controllers with @WebMvcTest.

Web Layer Tests with MockMvc is a free Spring Boot 4 Microservices & REST APIs lesson on CoddyKit — lesson 2 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 Microservices & REST APIs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Testing the Web Layer

Web layer tests verify your controllers — request mapping, validation, serialization, and status codes — without starting a full server.

Spring Boot provides @WebMvcTest and MockMvc for this.

@WebMvcTest

@WebMvcTest loads only the web slice: controllers, filters, and converters. Services and repositories are not loaded — you mock them.

@WebMvcTest(UserController.class)
class UserControllerTest {
    @Autowired
    MockMvc mockMvc;

    @MockBean
    UserService userService;
}

MockMvc.perform

MockMvc simulates HTTP requests against your controller without a network.

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

Mocking the Service

Use @MockBean to replace the service in the context, then stub it with Mockito.

@Test
void returnsUser() throws Exception {
    when(userService.getById("1"))
        .thenReturn(new User("1", "Alice"));

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

Asserting JSON with jsonPath

jsonPath verifies fields in the JSON response body.

mockMvc.perform(get("/users/1"))
    .andExpect(status().isOk())
    .andExpect(jsonPath("$.name").value("Alice"))
    .andExpect(jsonPath("$.id").value("1"));

Testing POST Requests

Send a JSON body with content and contentType.

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

Asserting Error Statuses

Verify 404 or 400 responses for missing resources or invalid input.

when(userService.getById("99"))
    .thenThrow(new NotFoundException());

mockMvc.perform(get("/users/99"))
    .andExpect(status().isNotFound());

Testing Validation

When a request fails bean validation, expect a 400 Bad Request.

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

Checking Headers

Assert response headers such as Location after a create.

mockMvc.perform(post("/users")
    .contentType(MediaType.APPLICATION_JSON)
    .content("{\"name\":\"Bob\"}"))
    .andExpect(header().string("Location",
        "/users/2"));

Query Parameters

Add request parameters with param.

mockMvc.perform(get("/users")
    .param("name", "Alice"))
    .andExpect(status().isOk())
    .andExpect(jsonPath("$.length()").value(1));

Printing the Result

Use andDo(print()) to log the full request and response while debugging.

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

Quick Check

Test your understanding of MockMvc.

Recap

You learned to test controllers in isolation:

  • @WebMvcTest loads only the web slice
  • MockMvc.perform simulates requests
  • @MockBean replaces services
  • jsonPath, status(), header() for assertions

Frequently asked questions

Is the “Web Layer Tests with MockMvc” lesson free?

Yes — the full text of “Web Layer Tests with MockMvc” is free to read here on the web, and the Spring Boot 4 Microservices & REST APIs 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 Microservices & REST APIs course, upgrade to CoddyKit PRO.

What will I learn in “Web Layer Tests with MockMvc”?

Test controllers with @WebMvcTest. You practise Spring Boot 4 Microservices & REST APIs 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 Microservices & REST APIs?

No prior experience is required. Spring Boot 4 Microservices & REST APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Web Layer Tests 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 Microservices & REST APIs lesson?

Yes. Every Spring Boot 4 Microservices & REST APIs 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 and Mockito
  2. Web Layer Tests with MockMvc
  3. Integration Tests with @SpringBootTest
  4. Real Dependencies with Testcontainers
← Back to Spring Boot 4 Microservices & REST APIs