I am running into a problem when two objects return true for .equals()
and return the same exact hashcode but assertEquals throws
AssertionFailedErrorException.
Since they return true for .equals and hashcode is exactly the same, I
am confused why assertEquals barks.
Any ideas?
Oliver Wong - 03 Aug 2006 18:31 GMT
>I am running into a problem when two objects return true for .equals()
> and return the same exact hashcode but assertEquals throws
[quoted text clipped - 4 lines]
>
> Any ideas?
Can you post an SSCCE? http://mindprod.com/jgloss/sscce.html
My guess it it has something to do with the overloading of the equals()
method, but I can't say much more without seeing some code.
As an ugly workaround, you could try assertTrue(obj1.equals(obj2)), but
you'd probably be masking a bug somewhere (whether that bug lies in your
code, in jUnit, or in the JVM remains to be seen).
- Oliver
Eric Sosman - 03 Aug 2006 19:08 GMT
djgavenda2@yahoo.com wrote On 08/03/06 13:04,:
> I am running into a problem when two objects return true for .equals()
> and return the same exact hashcode but assertEquals throws
> AssertionFailedErrorException.
>
> Since they return true for .equals and hashcode is exactly the same, I
> am confused why assertEquals barks.
A suspicion: Does your class implement
public boolean equals(Object obj) { ... }
or does it instead implement
public boolean equals(YourClass obj) { ... }
? If it's the latter, note that this is *not* the method
JUnit will use (nor will most of the rest of Java).

Signature
Eric.Sosman@sun.com
AndrewMcDonagh - 03 Aug 2006 19:26 GMT
> djgavenda2@yahoo.com wrote On 08/03/06 13:04,:
>> I am running into a problem when two objects return true for .equals()
[quoted text clipped - 14 lines]
> ? If it's the latter, note that this is *not* the method
> JUnit will use (nor will most of the rest of Java).
I'd have the same suspicion too, as assertEquals(....) have been in use
for 5 years now* - we'd no by now if it didn't work.
* This assumes the op is using junit 3.8.x
If they are using Junit 4.1 then its possible (though IMO unlikely they
have found a bug)
djgavenda2@yahoo.com - 03 Aug 2006 19:48 GMT
> djgavenda2@yahoo.com wrote On 08/03/06 13:04,:
> > I am running into a problem when two objects return true for .equals()
[quoted text clipped - 17 lines]
> --
> Eric.Sosman@sun.com
Eric,
Yep...that's it. My bad...thanks for the help. I was quite sure it
wasn't a bug w/ junit b/c there would have been more posts about it
then.
Thanks again...
AndrewMcDonagh - 03 Aug 2006 19:27 GMT
> I am running into a problem when two objects return true for .equals()
> and return the same exact hashcode but assertEquals throws
[quoted text clipped - 4 lines]
>
> Any ideas?
have you debugged into it?
AndrewMcDonagh - 03 Aug 2006 19:42 GMT
> I am running into a problem when two objects return true for .equals()
> and return the same exact hashcode but assertEquals throws
[quoted text clipped - 4 lines]
>
> Any ideas?
no idea, thie follow simple test shows assertEquals works ok .
package com.amd.sandbox;
import junit.framework.TestCase;
public class EqualsTest extends TestCase {
public void testEqualsWhereHashcodeIsSameAndEqualsReturnsTrue() {
Foo f1 = new Foo("hello");
Foo f2 = new Foo("hello");
assertEquals("failed!", f1, f2);
assertEquals("Hashcode differs!", f1.hashCode(), f2.hashCode());
}
public void testNotEqualWhenMessageDifferent() {
Foo f1 = new Foo("hello");
Foo f2 = new Foo("bye");
assertFalse("failed!", f1.equals(f2));
assertFalse("Hashcode the same!", f1.hashCode() == f2.hashCode());
}
private static class Foo {
private String message;
public Foo(String aMessage) {
message = aMessage;
}
public boolean equals(Object obj) {
Foo other = (Foo) obj;
if (this.message.equals(other.message))
return true;
return false;
}
public int hashCode() {
return message.hashCode();
}
}
}