Problem with Junit test

D

djgavenda2

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?
 
O

Oliver Wong

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?

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
 
E

Eric Sosman

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).
 
A

AndrewMcDonagh

Eric said:
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).

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)
 
A

AndrewMcDonagh

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?

have you debugged into it?
 
A

AndrewMcDonagh

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?


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();
}
}

}
 
D

djgavenda2

Eric said:
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).

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top