3

I decided to use REST-Assured for testing REST services. The results are (mostly) JSON and XML documents. For checking JSON, I find JSONassert very helpful.

I do not want to compare parts of the body, but the complete body of a response. So, the examples at REST-Assured's documentation are not very helpful, as they extract parts of the response body.

Currently, I am extracting the body and compare the body afterwards:

public void assertGet(String restURL, String fileName) throws Exception {
    String expectedStr = readFromClasspath(fileName);
    final String receivedStr = given()
            .log()
            .ifValidationFails()()
            .accept(getAccept(fileName))
            .get(callURL(restURL))
            .then()
            .log()
            .ifValidationFails()
            .statusCode(200)
            .extract()
            .response()
            .getBody()
            .asString();
    if (isXml(fileName)) {
        org.hamcrest.MatcherAssert.assertThat(receivedStr, CompareMatcher.isIdenticalTo(expectedStr).ignoreWhitespace());
    } else {
        JSONAssert.assertEquals(
                expectedStr,
                receivedStr,
                true);
    }
}

The issue is that ifValidationFails() does not make any sense, because asString() is always successful.

Now I wonder how I can include the JSONAssert.assertEquals and Hamcrest's assertThat into the given() method of REST-Assured to enable logging on failing validation.

Full source is available at Eclipse Winery's repository.

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
koppor
  • 19,079
  • 15
  • 119
  • 161

0 Answers0