liveBook · Manning

MockMvc

As explained in Spring Start Here, Second Edition

MockMvc is a Spring testing API that performs requests against Spring MVC controllers without starting a real HTTP server.

MockMvc is a Spring testing API for calling Spring MVC controllers without starting a real HTTP server. It lets controller integration tests check an endpoint’s contract, including its URL, request, response, and validation behavior. section 16.2

A typical test follows two steps:

  • Configure a MockMvc instance using the web application context. section 16.2
  • Send a request and check the response. Choose which objects in the request-processing flow are real and which are replaced with mocks. section 16.2

Testing a money transfer

A test can send a JSON POST request to /transfer with sender and receiver IDs and an amount, then assert that the response status is OK. section 16.2

Figure 16.2 A controller integration test. A tool such as MockMvc lets the test execute the web layer without starting a real server. The test sends a simulated HTTP request to the controller, while the developer decides which objects in the request-processing flow are real and which ones are replaced with mocks.

The diagram shows a simulated request reaching the web layer without a real server, with real or mocked objects in the request-processing flow. section 16.2

How Spring Start Here, Second Edition covers it

How deep it goes
Demonstrates client configuration, request construction, and response-status checks without detailing MockMvc internals.
Examples
Uses a JSON POST to /transfer and checks for an OK response.
What you can do after
Apply MockMvc to send endpoint requests and validate responses without starting a real HTTP server.
How it connects
Connects controller integration testing with Spring MVC, TestRestTemplate, and WebTestClient.

Also explained in

Compare how 2 books explain MockMvc →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.