I am using JSON Unit to compare two json responses. However the UUID's are dynamic everytime so the responses are always going to be different, the following below doesn't ignore the "id" and always flags this as different.
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;
}
}
Here is a small example of the JSON:
{
"item": [
{
"id": "1",
"title": "Hello"
}
]
}
Solved the above with the following:
JsonAssert.setOptions(IGNORING_VALUES);