json() is a WebTestClient assertion that compares an HTTP response body with an expected JSON string. It helps verify complete, nested responses when checking individual fields with jsonPath() becomes cumbersome. section 12.3.1
Use json() in a response assertion chain:
- Load the expected JSON, often from a classpath resource such as /tacos/recent-tacos.json. section 12.3.1
- Convert the resource to a String with Spring's StreamUtils.copyToString(). section 12.3.1
- Call expectBody().json(expectedJson) after exchanging the request and asserting its HTTP status. section 12.3.1
Checking recent tacos
A WebTestClient request to /api/tacos?recent can require application/json, expect an OK response, and compare the entire response with recentsJson using expectBody().json(recentsJson).
