【发布时间】:2018-06-05 09:44:12
【问题描述】:
我正在使用 JSON Unit 来比较两个 json 响应。 然而,UUID 每次都是动态的,因此响应总是会有所不同,下面的内容不会忽略“id”并且总是将其标记为不同。
private void assertWithIgnore(String endpoint, String expected, int code) throws IOException {
try {
response.then()
.statusCode(code)
.contentType(ContentType.JSON);
if (compare) {
assertJsonEquals(response.asString(),
resource("expected/" + expected),
JsonAssert.whenIgnoringPaths("item[*].id"));
} else {
Assert.fail("Not comparing response, write direct to file!");
}
} catch (AssertionError error) {
FileUtils.writeStringToFile(getResultFile(expected), response.prettyPrint());
failures.put(baseURL + basePath + endpoint, response);
throw error;
}
}
这里是 JSON 的一个小例子:
{
"item": [
{
"id": "1",
"title": "Hello"
}
]
}
【问题讨论】:
-
确保您使用的是最新版本的
JsonUnit依赖项并查看此 post -
哪个帖子?我使用的是 1.29.1 版
-
点击我超链接的帖子。
-
我已经尝试过这里的解决方案.. 仍然选择那些不同的 id。