17

I'm trying to test a API with Rest Assured. There is an AssertionError when I'm checking a double value.

The code for checking the double:

given().body(getTest()).contentType("application/json\r\n").
            when()
            .port(port)
            .basePath("/fff/test")
            .post("insert")
            .then()
            .assertThat()
            .statusCode(200)
            .body("versie", equalTo(11.0));

This is the output:

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

When I change the line with .body to:

.body("versie", equalTo(""+11.0));

The output is:

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

Does anyone know how I can fix this? Because I really don't know how to solve this.

EDIT
The JSON:

{ 
  "id": 1,
  "naam": "Test X",
  "versie": 11.0
}
476rick
  • 2,764
  • 4
  • 29
  • 49

2 Answers2

20
.body("versie", equalTo(11.0f));

This did work for me.
The answer is based on a comment from @StanislavL.

476rick
  • 2,764
  • 4
  • 29
  • 49
2

try with a cast (float) into equalTo - .body("value", equalTo((float)12.9)

Joseph_lpz
  • 21
  • 2
  • This resulted from a code I was developing, where the current response was 1.2, but with the float data I expected a <1.2> when casting the data (float) this result – Joseph_lpz Jul 29 '20 at 21:54