I was playing around with the Gson and JSONAssert libraries when I ran into this problem.
I have the following function to compare two integers:
private static void runCompareInts(int source, int target){
JSONCompareResult result = JSONCompare.compareJSON(new Gson().toJson(source),
new Gson().toJson(target),
JSONCompareMode.NON_EXTENSIBLE);
if (CollectionUtils.isEmpty(result.getFieldFailures())) {
System.out.println("Objects are same.");
} else {
System.out.println("Objects are not the same. Difference: " + result);
}
}
When I run runCompareInts(1, 2)
, I get "Objects are same."
as the result, which should not be the case.
I found that new Gson().toJson(1)
returns the String "1"
, which is a valid JSON string and hence the comparison should proceed correctly and go into the else
block.
Comparing the integers using JSONAssert.assertNotEquals("1", "2", true)
does not result in any exception. This implies that the Gson converted values are not the problem.
Can anyone tell me what the error with my runCompareInts()
function is? Thank you.
EDIT: JSONAssert.assertNotEquals(new Gson().toJson(source), new Gson().toJson(target), true)
also works fine.
Use
result.isFailureOnField()
inside if block.I see an issue with messages in jsonassert library, method
compareJson(final JSONString expected, final JSONString actual)
ofJSONCompare
class is returning empty error message on failure.