Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for strict JSON comparison in WebTestClient #27993

Closed
wants to merge 4 commits into from

Conversation

elgleidson
Copy link
Contributor

@elgleidson elgleidson commented Feb 1, 2022

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

@pivotal-cla
Copy link

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

@pivotal-cla
Copy link

@elgleidson Thank you for signing the Contributor License Agreement!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 1, 2022
@sbrannen
Copy link
Member

sbrannen commented Feb 1, 2022

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?

@sbrannen sbrannen added in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement labels Feb 1, 2022
@sbrannen sbrannen changed the title WebTestClient: support for strict JSON comparison Add support for strict JSON comparison in WebTestClient Feb 1, 2022
@sbrannen sbrannen removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 1, 2022
@sbrannen sbrannen added this to the 5.3.16 milestone Feb 1, 2022
@elgleidson
Copy link
Contributor Author

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.

@sbrannen sbrannen self-requested a review February 1, 2022 13:09
@sbrannen sbrannen self-assigned this Feb 1, 2022
@sbrannen sbrannen removed their request for review February 5, 2022 18:20
sbrannen added a commit to sbrannen/spring-framework that referenced this pull request Feb 5, 2022
@sbrannen sbrannen closed this in 920be8e Feb 5, 2022
@sbrannen
Copy link
Member

sbrannen commented Feb 5, 2022

This has been merged into 5.3.x and main in 920be8e and slightly revised in eb84c84.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants