Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Problem with Junit test

Thread view: 
djgavenda2@yahoo.com - 03 Aug 2006 18:04 GMT
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();
    }
  }

}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.