Search code examples
rest-assured-jsonpath

When I attempt to match a double value from a json array it fails with the expected value having angle brackets around it. What does this mean?


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?


Solution

  • 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]));