1

I'm using org.json to convert XML to JSON and I want to unit test (using JUnit) my parsing process.
However, when I try to compare the objects using json.equals it fails. I understand that jackson has implemented the equals method properly. Is there a way to unit test the equality of the objects with org.json?

...
JSONObject actual = XML.toJSONObject(xml);
JSONObject expected = new JSONObject().put("key","val");
assertEquals(expected, actual); // false
assertEquals(expected.toString(), actual.toString()) // true
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
itaied
  • 6,827
  • 13
  • 51
  • 86
  • take a look at this question, maybe some of the answers can give you hints on how to resolve your issue : http://stackoverflow.com/questions/2253750/compare-two-json-objects-in-java – francesco foresti May 16 '16 at 14:11

2 Answers2

1

You need to use JSONassert, avialable at github https://github.com/skyscreamer/JSONassert

chrisl08
  • 1,658
  • 1
  • 15
  • 24
1

You can also try using ModelAssert - https://github.com/webcompere/model-assert - this would look like so:

JSONObject actual = XML.toJSONObject(xml);
JSONObject expected = new JSONObject().put("key","val");
assertJson(actual)
   .isEqualTo(expected);

However, there's a chance that things such as key order may be affected by the conversion to JSONObject. In that case you can relax key order:

assertJson(actual)
   .where().keysInAnyOrder()
   .isEqualTo(expected);
Ashley Frieze
  • 4,993
  • 2
  • 29
  • 23