I created a class and overridden the equals() method. When I use assertTrue(obj1.equals(obj2)), it will pass the test; however, assertEquals(obj1, obj2) will fail the test. Could someone please tell the reason why?
I created a class and overridden the equals() method. When I use assertTrue(obj1.equals(obj2)) ,
Share
My guess is that you haven’t actually overridden
equals– that you’ve overloaded it instead. Use the@Overrideannotation to find this sort of thing out at compile time.In other words, I suspect you’ve got:
where you should have:
In your working assertion, you were no doubt calling the overloaded method as the compile-time type of
obj1andobj2were bothMyClass(or whatever your class is called). JUnit’sassertEqualswill only callequals(Object)as it doesn’t know any better.