Support for expectedBody().json(<expected strict json>, true);

Part of the code base already has support - via JsonExpectationsHelper - but WebTestClient has no method to use the strict version. The existing method uses lenient, which is not always desired.

Example: Let's say /persons/1/ returns the following response:

{
  "name":"John",
  "surname":"Smith"
}

The following test works fine:

webTestClient.get()
    .uri("/persons/1")
    .accept(MediaType.APPLICATION_JSON)
    .exchange()
    .expectStatus().isOk()
    .expectBody().json("{\"name\":\"John\"}");

I'm just adding a new method so that we can use strict comparison when appropriate.

Example:

This test fails because the response has more fields than expected and we're using strict mode:

webTestClient.get()
    .uri("/persons/1")
    .accept(MediaType.APPLICATION_JSON)
    .exchange()
    .expectStatus().isOk()
    .expectBody().json("{\"name\":\"John\"}", true);

But the following test passes because we're expecting all fields:

webTestClient.get()
    .uri("/persons/1")
    .accept(MediaType.APPLICATION_JSON)
    .exchange()
    .expectStatus().isOk()
    .expectBody().json("{\"name\":\"John\",\"surname\":\"Smith\"}", true);

Comment From: pivotal-cla

@elgleidson Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

Comment From: pivotal-cla

@elgleidson Thank you for signing the Contributor License Agreement!

Comment From: sbrannen

It looks like the code examples for The following test works fine and This test fails are identical. Can you please update this PR's description?

Comment From: elgleidson

It looks like the code examples for The following test works fine and This test fails are identical. Can you please update this PR's description?

Done.

Comment From: sbrannen

This has been merged into 5.3.x and main in 920be8e1b201fa947b07941eeace62ecf0f431a9 and slightly revised in eb84c843736aaa555d9078a544280bd70b714fbb.

Thanks for making your first contribution to the Spring Framework! 👍