JsonApprovals.verifyAsJson fails when fields are not in the same order between tests
horiaconstantin-cpi opened this issue ยท 3 comments
Data that I use:
Map<String, String> test = new HashMap<>(); test.put("2test", "test"); test.put("1test", "test");
when I use JsonApprovals.verifyAsJson, it creates an output file as expected.
However, if I sort the keys in the map (test1, test2), the test fails. Since JSON field order is not mandatory, I think that the test should be passing.
What do you think?
if the order is different, the approval will fail.
We can enforce an ordering, but it sounds like you want to change the ordering and have it still pass. is that correct?
I can confirm that the behavior that you describe in the first sentence is what I'm also seeing.
Indeed, I want the approval to pass if the ordering of the JSON fields changes. Or I'd like a way to control if the verify is lenient regarding ordering. GSONBuilder doesn't offer that
- we're not about to fix that
- we're gonna give you a method to fix it :)
with release 18.5.0 (releasing right now) there is now a new configuration for Options
that allows you to provide a custom comparator.
example:
@Test
void verifyCustomComparator()
{
Approvals.verify("The approval file is empty", new Options().withComparator((a, b) -> VerifyResult.SUCCESS));
}
So your custom implementation would probably have to read files a and b into a JsonObject from gson and then does gson offer an equals
method to tell you that they are the same, even with different order?!