0Pricing
Spring Boot 4 Complete Guide · Lektion

Web-Controller mit MockMvc testen

Schreiben Sie fokussierte Tests für Ihre REST-Endpunkte mit MockMvc, um HTTP-Anfragen zu simulieren, ohne einen echten Server zu starten.

Web-Controller mit MockMvc testen ist eine kostenlose Spring Boot 4 Complete Guide-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Spring Boot 4 Complete Guide-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Spring Boot 4 Complete Guide-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.

Häufig gestellte Fragen

Ist die Lektion „Web-Controller mit MockMvc testen“ kostenlos?

Ja — der vollständige Text von „Web-Controller mit MockMvc testen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Spring Boot 4 Complete Guide-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Spring Boot 4 Complete Guide-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Web-Controller mit MockMvc testen“?

Schreiben Sie fokussierte Tests für Ihre REST-Endpunkte mit MockMvc, um HTTP-Anfragen zu simulieren, ohne einen echten Server zu starten. Du übst Spring Boot 4 Complete Guide mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Spring Boot 4 Complete Guide zu starten?

Keine Vorkenntnisse erforderlich. Spring Boot 4 Complete Guide auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Web-Controller mit MockMvc testen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Spring Boot 4 Complete Guide-Lektion Code schreiben und ausführen?

Ja. Jede Spring Boot 4 Complete Guide-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Unit-Tests mit JUnit und Mockito
  2. Integrationstests mit Spring Boot
  3. Slice-Tests & TestContainers
  4. Web-Controller mit MockMvc testen
← Zurück zu Spring Boot 4 Complete Guide