Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assertEquals fails for equivalent Jackson objects created in different ways

So I am wondering why two objects are failing in my unit test (Junit 5) when they are created differently.

First way:

static ObjectMapper mapper = new ObjectMapper();
JsonNode output = mapper.convertValue(jsonTransform, JsonNode.class);

Second way:

JsonNode expectedOutput = mapper.readTree(jsonString);

And then asserted:

Assertions.assertEquals(expectedOutput, output);

The failure:

is org.opentest4j.AssertionFailedError: expected: com.fasterxml.jackson.databind.node.ObjectNode@d6e7bab<jsonString> but was: com.fasterxml.jackson.databind.node.ObjectNode@5fa07e12<jsonString>

Further, if I then edit the assertion to be:

Assertions.assertEquals(expectedOutput, mapper.readTree(output.toString()));

It will pass. So what concept is causing the first assertion to fail?

like image 566
user2820906 Avatar asked Oct 31 '25 00:10

user2820906


1 Answers

I'm having the same issue, the only solution I have found so far is to make use of:

Assertions.assertEquals(expectedOutput.toPrettyString(), output.toPrettyString());

I know it's not optimal but does the job. Funny thing is InteliJ says that the "Contents are identical" on the Comparison Failure but the test fails if I remove the toPrettyString()

like image 193
Luis Valdez Avatar answered Nov 02 '25 15:11

Luis Valdez