liveBook · Manning

Json()

As explained in Spring in Action, Sixth Edition

json() is a WebTestClient assertion operation that compares a response body with an expected JSON string.

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).

How Spring in Action, Sixth Edition covers it

How deep it goes
Defines full-body comparison and demonstrates loading expected JSON, but does not detail JSON matching internals.
Examples
Tests the recent-tacos.json response for a GET request to /api/tacos?recent.
What it assumes
Assumes familiarity with WebTestClient request chains and teaches expected JSON loading from a classpath resource.
What you can do after
Evaluate a reactive GET response by comparing its complete body with an expected JSON document.
How it connects
Connects WebTestClient with expectBody(), jsonPath(), expectStatus(), StreamUtils, and expectBodyList().

Related concepts

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