0

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"
}
]
}
mvoase
  • 544
  • 1
  • 6
  • 20

2 Answers2

0

Solved the above with the following:

JsonAssert.setOptions(IGNORING_VALUES);
mvoase
  • 544
  • 1
  • 6
  • 20
0

With JsonAssert, this should also work...ready?

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);

I'm using library version 1.5.0

Martin
  • 41
  • 3