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

Document using AssertJ's satisfies together with extractingJsonPathNumberValue #16229

Closed
wimdeblauwe opened this issue Mar 14, 2019 · 2 comments
Labels
type: documentation A documentation update
Milestone

Comments

@wimdeblauwe
Copy link
Contributor

I am using extractingJsonPathNumberValue to test a double value in my JSON:

 assertThat(json.write(message))
   .extractingJsonPathNumberValue("@.stopTime.percentage").isEqualTo(0.16666666666666666)

However, this does not work because of different types:

org.opentest4j.AssertionFailedError:
Expecting:
<"0.16666666666666666 (BigDecimal@1c52552f)">
to be equal to:
<"0.16666666666666666 (Double@5dc769f9)">
but was not.
Expected :0.16666666666666666
Actual :0.16666666666666666

What is the recommended way to do this? Should there be other methods like extractingJsonPathDoubleValue or extractingJsonPathFloatValue that could return DoubleAssert or FloatAssert so that methods like isCloseTo can be used?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 14, 2019
@wilkinsona
Copy link
Member

We can consider adding some more methods, but I'm not sure that we need to increase the surface area of the API. You can use AssertJ's satisfies and your knowledge of the type of the Number to then use isCloseTo:

assertThat(json.write(message))
    .extractingJsonPathNumberValue("@.stopTime.percentage")
    .satisfies((number) -> assertThat(number.floatValue()).isCloseTo(0.16f, within(0.01f)));

@wimdeblauwe
Copy link
Contributor Author

Using satisfies is indeed a very good way to do it. Maybe this could be added to the https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-testing-autoconfigured-json-tests documentation instead of adding those extra methods to the API ?

@mbhave mbhave added type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 16, 2019
@mbhave mbhave added this to the 2.1.x milestone Mar 16, 2019
@mbhave mbhave changed the title Best way to test double values with extractingJsonPathNumberValue? Document using AssertJ's satisfies together with extractingJsonPathNumberValue Mar 16, 2019
@mbhave mbhave modified the milestones: 2.1.x, 2.1.4 Mar 20, 2019
@mbhave mbhave closed this as completed in 4a230fd Mar 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

4 participants