Open marcospassos opened 7 years ago
According to this source, it's simple as output both documents using a specific format:
Hi, the trouble is that JSON document does not have predefined order. In other words what should get displayed as diff when comparing {a:1, b:2} and {b:1, a:2}? In order to show string-like diff we would need to somehow normalize the JSON. And it's tricky, it basically means to be able to sort generic object tree.
Yeah, I know, but for comparison purposes we can sort the keys, i.e alphabetical ascending order, or even sort the compared version according to the reference (preferred). In the later case, we can use a comparator that sorts looking to the index of the same key in the reference JSON document.
In our use case, we can save about 1 minute per broken test by comparing both JSON documents straight in the IDE.
I will think about it. The trouble is that it has to play well with other features like ${json-unit.ignore} and ignoring array order and other stuff.
It is a work-around, as it will show all the differences, not only the json unit detected differences, but I use the following code to show IntelliJ comparator if there is an issue :
public static void assertThatAndShowIntelliJComparator(String expected, String actual, Matcher matcher) { try { assertThat(actual, matcher); } catch (AssertionError e) { Assert.assertEquals(e.getMessage(), expected, actual); } }
I still need to use workaround to see full json:
try {
assertThatJson(actualJson)
.isEqualTo(expectedJson);
} catch (AssertionError e) {
Assert.assertEquals(e.getMessage(), expectedJson, actualJson);
}
Can you fix this?
Hi @Hronom, @ndebeiss, @marcospassos I am currently looking into that. I am curious what would be your expectation, we can to the following
From my side it will be ok if we can have just printed actual and expected json's as is.
hello Sorry, I don't have the use case anymore, but I would vote for option 1. Option 2 seems difficult. Option 3 and option 4 would override the IDE behavior. many thanks for the project !
Is there any way to see the json diff, exactly as when we assert strings? Using ordinary assertions on IntelliJ allows me to compare two jsons when an assertion fails. It's much more efficient for identifying the differences in long json files.