1

I have a test that I cannot get the syntax correctly:

@Test
void statsTest() {
    given().queryParam("param", "ball")
            .when().get()
            .then().body("total", is(closeTo(10.0, 0.1*10.0))));
}

However, the test keeps failing even though the condition is met:

java.lang.AssertionError: 1 expectation failed.
JSON path total doesn't match.
Expected: is a numeric value within <1.0> of <10.0>
Actual: 10

I've never had a problem with types before in this setup of RestAssured and Hamcrest. For example, a test of the sort: body("total", greaterThan(9)) works fine, which means that there is some type casting under the hood.

I've looked through the docs and cannot find a way to cast the value of body("total") to a numerical value. so I suspect that this is a bug or I'm not understanding something here.

Here's the JSON response. I had to clip it to make is short. Hope this works.

{
 "stats": {
 "totalHits": 1,
 "searchEngineTimeInMillis": 83,
 "searchEngineRoundTripTimeInMillis": 87,
 "searchProcessingTimeInMillis": 101
},
 "products": {
    "id": "total",
    "displayName": "Documents",
    "ball": 10}
}
Shejo284
  • 4,541
  • 6
  • 32
  • 44

2 Answers2

2

The key value pair corresponding to key: "total" in your response seems to be of integer type. So it needs to be checked for bounds with integer based bounds (1,10). So instead of using the closeTo matcher, you can use the following matcher.

allOf(greaterThanOrEqualTo(1), lessThanOrEqualTo(10)))
SudhirR
  • 760
  • 4
  • 11
  • 1
    Thanks for the code. This thought was to check the bounds on both sides of the `10`, so I need to also check the range `[10, 20]`. Check out my solution above :-) – Shejo284 Mar 11 '19 at 06:00
1

I've put together another approach that solves the problem but with a slightly different approach. Much thanks to those who populate the web with their code samples. The following assumes you already have set the base URI and PATH. You can add a path deeper in the response by using the get("/path..."). This answer assumes a JSON type response.

 private static Response getResponse(String paramName, String paramValue) {
    return given().queryParam(paramName, paramValue)
            .when().get();
}

 public static String getJsonValue(String jsonPath, String paramName, String paramValue) {
    Response response          = getResponse(paramName, paramValue);
    //response.getBody().prettyPrint();
    JsonPath jsonPathEvaluator = response.jsonPath();
    return jsonPathEvaluator.get(jsonPath).toString();
}

You can simply print the return value and cast it to the type you need. The test then looks like this:

 public static void checkIfNumberCloseToValue(String jsonPath,
                                             String paramName,
                                             String paramValue,
                                             Double error,
                                             Double expected) {
    Double value = Double.valueOf(Utils.getJsonValue(jsonPath, paramName, paramValue));
    double range = expected * error;
    assertThat(value, closeTo(expected, range));
}
Shejo284
  • 4,541
  • 6
  • 32
  • 44