Using RestAssured I attempt to check the following piece of JSON:
{
"id": "abc1",
"commonName": "Plane",
"location": [
1.1,
1.1
]
}
with the following piece of java code:
double[] location = new double[]{1.1,1.1};
given()
.when()
.get("tree/abc1/")
.then()
.assertThat()
.statusCode(HttpStatus.OK.value())
.body("location[0]", is((location[0])));
The last assertion fails with the following error
java.lang.AssertionError: 1 expectation failed.
JSON path location[0] doesn't match.
Expected: is <1.1>
Actual: 1.1
What do the angle brackets around the expected value indicate and how can I get the assertion to succeed?
The default type for JSON numbers is float when using rest assured. I presume the angle brackets are indicating a type mismatch.
The solution is to set the rest assured configuration in the given block to specify the number type.
double[] location = new double[]{1.1,1.1};
given()
.config(RestAssured.config().jsonConfig(jsonConfig().numberReturnType(JsonPathConfig.NumberReturnType.DOUBLE)))
.when()
.get("tree/abc1/")
.then()
.assertThat()
.statusCode(HttpStatus.OK.value())
.body("location[0]", is(location[0]));