我正在使用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"
}
]
}
答案 0 :(得分:0)
用以下内容解决上述问题:
JsonAssert.setOptions(IGNORING_VALUES);
答案 1 :(得分:0)
有了JsonAssert,这应该也可以工作……准备好了吗?
ArrayValueMatcher<Object> arrayValueMatcher = new ArrayValueMatcher<>(new CustomComparator(JSONCompareMode.LENIENT, new Customization("item[*].id", new ValueMatcher<Object>() {
@Override
public boolean equal(Object o1, Object o2) {
return true;
}
})));
Customization arrayValueMatcherCustomization = new Customization("item", arrayValueMatcher);
CustomComparator customArrayValueComparator = new CustomComparator(JSONCompareMode.LENIENT, arrayValueMatcherCustomization);
JSONAssert.assertEquals(expect, actual, customArrayValueComparator);
我正在使用库版本1.5.0