4

I'm using restassured and org.hamcrest.Matchers. I have the following response from my api

{
  "entity" : {
    "value" : 1.0
  }
}

in my tests I have the following code

given()
  .headers(createHeaders())
.when()
  .get(url)
  .prettyPeek()
.then()
  .statusCode(HttpStatus.OK.value())
  .body("entity.value", is(1.0));

Which is failing with message

java.lang.AssertionError: 1 expectation failed.
JSON path entity.value doesn't match.
Expected: is <1.0>
  Actual: 1.0

What Matcher should I use in this code? On DTO level it is

BigDecimal value;
alekz
  • 116
  • 9
  • Possible duplicate of [Rest Assured: java.lang.AssertionError: JSON path body doesn't match](https://stackoverflow.com/questions/31803954/rest-assured-java-lang-assertionerror-json-path-body-doesnt-match) – Mebin Joe Mar 19 '19 at 12:11
  • Did you try body("entity.value", is("1.0")); | This should possibly work, the response returned by rest assured is a as a string but the value you compare against is int and so the error – Wilfred Clement Mar 19 '19 at 12:12
  • 1
    No. It's not a string. On the DTO it is BigDecimal I it helps – alekz Mar 19 '19 at 12:17
  • Ahh, How about this - body("entity.value", is(new BigDecimal(1.0)) – Wilfred Clement Mar 19 '19 at 12:21
  • No. But if you know the reason, then you can find right answer [https://stackoverflow.com/questions/46815071/confusion-over-rest-assured-floating-point-comparisons] – alekz Mar 19 '19 at 13:45
  • 3
    @alekz Can you try this, body("entity.value", is(1.0f)) – Vamsi Ravi Mar 19 '19 at 20:57
  • @alekz Try this one http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#equalTo(T) – bhusak Mar 20 '19 at 09:08
  • Here is the link to documentation: https://github.com/rest-assured/rest-assured/wiki/Usage#note-on-floats-and-doubles explaining this feature – alekz Apr 23 '19 at 08:53

0 Answers0