我决定使用REST-Assured来测试 REST 服务。结果(大部分)是 JSON 和 XML 文档。对于检查 JSON,我发现JSONassert非常有用。
我不想比较身体的各个部位,而是比较完整的身体反应。因此,REST-Assured 文档中的示例不是很有帮助,因为它们提取了响应正文的一部分。
目前,我正在提取身体并在之后比较身体:
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);
}
}
问题是这ifValidationFails()
没有任何意义,因为asString()
总是成功的。
现在我想知道如何将JSONAssert.assertEquals
和 Hamcrest包含assertThat
到given()
REST-Assured 的方法中以启用失败验证的日志记录。
完整的源代码可在Eclipse Winery 的存储库中获得。