0

Using rest-assured to invoke a base rest service with:

  given().get(baseUrl + "/base/")
                .then()
                .statusCode(200)
                .body("size()", is(2))
                .body("meanPerDay", equalTo(1.5))

returns :

java.lang.AssertionError: 1 expectation failed.
JSON path meanPerDay doesn't match.
Expected: <1.5>
  Actual: 1.5

The payload of baseUrl + "/base/" is:

{
    "meanPerDay": 1.5,
    "stdPerDay": 0.5
}

If I replace .body("meanPerDay", equalTo(1.5)) with .body("meanPerDay", equalTo("1.5"))

the failure is:

java.lang.AssertionError: 1 expectation failed.
JSON path meanPerDay doesn't match.
Expected: 1.5
  Actual: 1.5

I'm not accessing the meanPerDay attribute correctly?

The test is finding the attribute value as the Expected is value 1.5?

blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • 2
    I think that the type of the expected value is not the same of the type of actual value. – Stefano Curcio Sep 03 '20 at 09:47
  • 1
    Take a look at https://stackoverflow.com/questions/56114915/how-to-compare-assert-double-values-in-rest-assured – Stefano Curcio Sep 03 '20 at 09:53
  • @StefanoCurcio Using .body("meanPerDay", Matchers.equalTo(Double.valueOf(1.5))); causes error: java.lang.AssertionError: 1 expectation failed. JSON path meanPerDay doesn't match. Expected: <1.5> Actual: 1.5 , answer provided by Peter Quan seems to work but I'm not sure why using Matchers is causing a failure. – blue-sky Sep 03 '20 at 10:02

1 Answers1

1

The below numbers are floating point

{
    "meanPerDay": 1.5,
    "stdPerDay": 0.5
}

, however, the following test is comparing with a "double":

 .body("meanPerDay", equalTo(1.5))

So, you can try this:

 .body("meanPerDay", equalTo(1.5f))
Peter Quan
  • 788
  • 4
  • 9