As of now, there are many tests that assert on toString() of objects.
This style is prone to unnecessary maintenance of the test code when upgrading dependencies. Dependencies may change the internal ordering of fields and trivial change in toString(). In BEAM-8695, where I tried to upgrade google-http-client, there are ~30 comparison failure due to this toString assertions.
They are subclasses of com.google.api.client.json.GenericJson.
Several options to enhance these assertions.
Leveraging the fact that GenericJson is a subclass of AbstractMap<String, Object>, the assertion can be written as
Credit: Ben Whitehead.
Leveraging the fact that instance of GenericJson can be instantiated through JSON, the assertion can be written as
assertEqualsOnJson is implemented as below. The following field and methods should go to shared test utility class (sdks/testing?)
A feature request to handle escaping double quotes via JacksonFactory: https://github.com/googleapis/google-http-java-client/issues/923
- https://github.com/hertzsprung/hamcrest-json (Not using as last commit was in 2012)
The JSONassert example does not carry quoted double quote characters. The implementation would be converting actual object into JSON object and calling JSONAssert.assertEqual.
Credit: Luke Cwik