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
1

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.