My guess is that you haven't actually overridden equals
- that you've overloaded it instead. Use the @Override
annotation to find this sort of thing out at compile time.
In other words, I suspect you've got:
public boolean equals(MyClass other)
where you should have:
@Override // Force the compiler to check I'm really overriding something
public boolean equals(Object other)
In your working assertion, you were no doubt calling the overloaded method as the compile-time type of obj1
and obj2
were both MyClass
(or whatever your class is called). JUnit's assertEquals
will only call equals(Object)
as it doesn't know any better.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…