Menu

#14 testEqualsAgainstNewObject has arguments reversed

Latest_release
open
nobody
5
2004-10-12
2004-10-12
No

The testEqualsAgainstNewObject method in the
EqualsHashCodeTestCase class calls Object.equals
instead of the equals method of the class being tested.
For example, here is one of the assertions:

Assert.assertNotEquals(o, eq1);

This will eventually invoke

o.equals(eq1)

What you really want to be testing is

eq1.equals(o)

Discussion

  • Gillmer J. Derge

    Logged In: YES
    user_id=222276

    The attached file demonstrates the problem and shows the
    solution. Note that the "BadEquals" class returns true from
    its equals method when the argument is *not* an instance of
    BadEquals. That is, an instance is BadEquals is equal to
    every instance of any other class.

    The testEqualsAgainstNewObject test should fail, because
    BadEquals will claim to be equal to the new Object.
    Instead, that test succeeds because it not invoking the
    BadEquals equals method. The
    testEqualsAgainstNewObjectProperly test fails as expected.

     
  • Gillmer J. Derge

     

Log in to post a comment.