loose2200 / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

spy redefines equals and hashCode #241

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

The following tests will show the problem:
{{{
Point point = new Point(1, 1);
Point point2 = new Point(1, 1);
// OK, using Point.equals(Object)
assertTrue(point.equals(spy(point2)));
// FAIL equals is not symmetric for objects with spies
assertTrue(spy(point2).equals(point));
// FAIL as spy redefines hash code
assertEquals(spy(point2).hashCode(), point.hashCode());
}}}
What is the expected output? What do you see instead?
spy on an existing object shouldn't redefine equals and hashCode.

What version of the product are you using? On what operating system?
Mockito 1.8.5

Please provide any additional information below.

Original issue reported on code.google.com by ivankob...@gmail.com on 24 Jan 2011 at 1:44

GoogleCodeExporter commented 8 years ago
Not sure if it's possible to fix because mock.equals() is used internally in 
Mockito. We'll have look but I cannot promise it will be fixed :)

Thanks for reporting!

Original comment by szcze...@gmail.com on 7 Apr 2011 at 9:03

GoogleCodeExporter commented 8 years ago
Issue 305 has been merged into this issue.

Original comment by brice.du...@gmail.com on 24 Dec 2011 at 9:18

GoogleCodeExporter commented 8 years ago
This bug is realy a PITA when you have stored the original object in a HashMap 
and you want to access the value by your spy! The object overwrites hashCode 
and equals and both simply delegate to the internal name field. Why cannot the 
spy simply delegate hashCode and equals to the original object?

Original comment by corneliu...@googlemail.com on 13 Mar 2014 at 9:49

GoogleCodeExporter commented 8 years ago
Because a spy is a kind of mock, and two spy may not be the same nor equals 
while their internal values may be the same.

Also the rule of thumb is to don't mock value objects ! Instead I'd rather not 
use builders to make those value objects. If not possible then it means the 
code has a design problem.
And this has the benefit of not being caught by this spy tradeoff.

Original comment by brice.du...@gmail.com on 13 Mar 2014 at 2:13