2

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?

user1098798
  • 313
  • 3
  • 14

2 Answers2

3

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]));
user1098798
  • 313
  • 3
  • 14
1

Returning floats and doubles as BigDecimal

instead of having two different type of configuration, Convert all floats and doubles into BigDecimal and then Compare then in the hamcrest Matcher

enter image description here

Arpan Saini
  • 4,623
  • 1
  • 42
  • 50