liveBook · Manning

Endpoint contract

As explained in Spring Start Here, Second Edition

An endpoint contract is the specification of a web endpoint's URL, HTTP method, inputs, outputs, status, headers, and validation behavior.

An endpoint contract is the behavior a web endpoint promises its clients. A controller integration test checks that the endpoint is mapped correctly, handles the request as expected, runs validation, and returns the expected response. section 16.2

A controller test can catch problems that service tests alone might miss:

  • The endpoint is mapped incorrectly or the request body is not deserialized as expected. section 16.2
  • Validation does not run, or the response differs from what the client expects. section 16.2
  • MockMvc exercises the web layer without starting a real HTTP server; TestRestTemplate and WebTestClient are other options. section 16.2

Money transfer

A MockMvc test sends a JSON POST request to /transfer and expects an OK status; the scenario checks that both account balances are 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 controller while the test chooses which objects in the request-processing flow are real or mocked. section 16.2

How Spring Start Here, Second Edition covers it

How deep it goes
Explains mapping, deserialization, validation, and response failures, then demonstrates a MockMvc integration test.
Examples
Tests a money transfer by posting JSON to /transfer and expecting an OK status and updated account balances.
What you can do after
Apply it via MockMvc to check how an endpoint handles a request and what response it returns.
How it connects
Connects endpoint contracts with controllers, HTTP requests, request deserialization, validation, and integration testing.

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.