1

How does Assert.assertEquals(Object1, Object2) compare both objects ?

In java, when you compare object1 and object2 using Object1.equals(object2), they are not equal even though the content of the object is same.

Where as if you compare String1.equals(String2) this will show both string as same even though they are different objects.

String a = "hi there";
String b = "hi there";
a.equals(b) returns true


JSONObject j1 = (new JSONObject()).put("a","hi there");
JSONObject j2 = (new JSONObject()).put("a","hi there");
j1.equals(j2) does not return true;

Q) Why are objects not converted to binary stream and then not compared to check if they are same ?

q) Assert.assertEquals(j1,j2): assertion fails. why ?

pullCommitRun
  • 373
  • 6
  • 18
  • 1
    just a note `a` and `b` in your code are **not** "different objects", they reference the same instance of string: `a == b` will result in `true` – user85421 Aug 04 '19 at 06:33
  • 3
    `object1.equals(object2)` works fine if the type overrides `equals`. It looks like `JSONObject` doesn't, and it's as simple as that. – chrylis -cautiouslyoptimistic- Aug 04 '19 at 06:34
  • why not convert to binary? it is the developer that *should* decide when 2 objects are equal or not. why `assertEquals` fails? because the objects are not `equal` (according their implementation) – user85421 Aug 04 '19 at 06:40

2 Answers2

3

Assert.assertEquals uses Object::equals underneath to compare objects. So Object::equals method has to be overriden for the given class, if the comparing behaviour, for instances of this class, should be changed. You are using org.json.JSONObject and this implementation does not override equals method from Object class. Object::equals method has following implementation :

public boolean equals(Object obj) {
     return (this == obj);
}

so it basically checks if two references (this and obj point to the same object). That is why your second assertion fails.

Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63
3

You are mixing up different ideas.

One concept is known as identity: are two references pointing to the same object, that is, the same chunk of memory? This is what == tests, as in x == y where x and y are reference variables (pointers) rather than primitives.

The .equals method on the Object class has a different intention. That method asks if the meaning of two objects is the same. The method judges if the content of two objects are significantly the same. In some cases “significantly” means exactly the same for every value of every field. What that means exactly is up to the particular subclass inheriting from Object. Whoever writes the implementation of equals gets to decide on what should be compared between the two objects.

As for comparing binary streams, first of all, objects don’t have a binary stream. Furthermore, even two pieces of text can have the same meaning yet vary in their bits – just look up Unicode Normalization. Or even the simpler case where in some situations two pieces of text might be considered equivalent if we choose to ignore uppercase versus lowercase, ex: Dog and dog and DOG might all three be considered that same while each carries different octets.

Context is everything.

In java, when you compare object1 and object2 using Object1.equals(object2), they are not equal even though the content of the object is same.

Your statement simply cannot be made about all classes. You must look to each class’ implementation of equals. Class Foo might compare every last piece of state in the object, while class Bar might choose to compare only a single id field of type UUID while ignoring the value in all the rest of its fields.

Assert.assertEquals(j1,j2): assertion fails. why ?

I do not know. I’d have to look up the Javadoc for the implementation of the Object::equals method override for the particular class in question (JSONObject in your case).

The point is that you must look up the equals method documentation for each comparison you make.

Likewise, be sure to write clear documentation for every equals method override you implement.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154