Now lets see how we can check JSON assertions using JSONASSERT library. For that we need to have skyscreamer dependency in our project.
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
We can compare two Strings in different ways and assertions will fail or pass based on the methods we are using.
1.Linient Mode :- Lenient mode allows extra items to be added to an array. So if in below example course is present in first string and not in second test will still pass.
Also if ordering in the array is not same like in values y comes before x in actualValue but in expectedValue x comes before y even then the lenient test will pass.
String actualValue = "{id:1,name:\"John\",course:\"Math\",values=[\"x\",\"y\"]}";
String expectedValue = "{id:1,name:\"John\",values=[\"x\",\"y\"]}";
JSONAssert.assertEquals(expectedValue, actualValue,JSONCompareMode.LENIENT);
2.STRICT is reverse of lenient. i.e it will fail if extra items like course is added and it will also fail if ordering of items in values is not same i.e x comes before y or vice versa.
JSONAssert.assertEquals(expectedValue, actualValue,JSONCompareMode.STRICT);
3.STRICT_ORDER will fail is x comes before y in values array but it will allow extensibility i.e if courses are added in actualValue but not present in expected_value then STRICT_ORDER will still pass
String actualvalue1 ="{id:1,name:\"John\",course:\"CSE\", values=[\"x\",\"y\"]}";
String expectedvalue1 = "{id:1,name:\"John\",values=[\"x\",\"y\"]}";
JSONAssert.assertEquals(expectedvalue1, actualvalue1, JSONCompareMode.STRICT_ORDER);
4.NON_EXTENSIBLE as names suggests will not allow strings to be extended so it will fail if courses are added in actualValue but not present in expected_value but it has nothing to do with ordering i.e if x comes before y in values array it will pass
String actualvalue2 ="{id:1,name:\"John\",values=[\"x\",\"y\"]}";
String expectedvalue2 = "{id:1,name:\"John\",values=[\"y\",\"x\"]}";
JSONAssert.assertEquals(expectedvalue2, actualvalue2, JSONCompareMode.NON_EXTENSIBLE);