liveBook · Manning

Controller integration test

As explained in Spring Start Here, Second Edition

A controller integration test verifies a web controller's endpoint contract, including request handling and response behavior.

A controller integration test checks whether a web endpoint behaves as its clients expect. It exercises the endpoint contract—not just a Java method—including its URL, HTTP method, request data, and response behavior. section 16.2

A test client sends a request and lets you check the response. Choose a tool based on the application and how realistic the test needs to be:

  • MockMvc exercises Spring MVC controllers without starting a real HTTP server. section 16.2
  • TestRestTemplate sends real HTTP requests to an application running on a test port. section 16.2
  • WebTestClient is commonly used with reactive applications, and can also serve other testing scenarios. section 16.2

Testing a money transfer

With MockMvc, send a JSON POST request to /transfer containing sender and receiver account IDs and an amount, then assert that the response status is OK. The test can also check that both balances were updated. 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.

See how a simulated request reaches the web layer, with the choice of real objects and mocks left to the test. section 16.2

How Spring Start Here, Second Edition covers it

How deep it goes
Exercises the web layer through request and response checks, and compares test clients without prescribing one universally.
Examples
Uses a banking transfer request with account IDs and an amount, then checks the response status.
What you can do after
Apply MockMvc to send a JSON transfer request and assert its HTTP status.
How it connects
Connects endpoint contracts with Spring MVC, MockMvc, TestRestTemplate, WebTestClient, validation, and security.

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.