dhamini-poornachandra / mockito

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

spy object returns wrong value on equal with itself. #305

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. see attached files
2.
3.

What is the expected output? What do you see instead?
spy object should return true when equals is called with spy object as argument

What version of the product are you using? On what operating system?
latest maven

Please provide any additional information below.
see attached files for a test case.

Original issue reported on code.google.com by DeRijcke.Erik@gmail.com on 24 Dec 2011 at 12:16

Attachments:

GoogleCodeExporter commented 8 years ago
I know for a fact that simple self equality contract is respected, check 
yourself those two lines :
ArrayList spy = spy(new ArrayList());
assertTrue(spy.equals(spy));

You should know that spy actually don't keep an internal reference to your 
instance, instead it creates a new proxy of your type then copy its state.

Your code doesn't seem to compile so I can't see what could be wrong there.

Original comment by brice.du...@gmail.com on 24 Dec 2011 at 1:50

GoogleCodeExporter commented 8 years ago
Yes, but unless there's an appropriate equals method, a spy won't be equal to 
the  object that it was made from.  So the following normally fails.

MyClass obj = new MyClass();
assertTrue( spy( obj ).equals( obj ));

I think that's what the original poster is getting at.  The only way I can 
think of to fix this would be to have some magic in the spy() method that 
provides an implementation of equals, if there isn't one already defined.  I 
can imagine all sorts of issues with doing this; it could be a dangerous thing 
to try.  Worth discussing though.

Original poster - if I've misunderstood your code, and the issue is actually 
something different from this, can you please try to construct a simpler 
example?  One that doesn't involve eight different related types?  Otherwise, 
helping you out becomes very difficult.

Original comment by dmwallace.nz on 24 Dec 2011 at 2:59

GoogleCodeExporter commented 8 years ago
Spy is just a special Mock.
So anyway equals method has a special meaning in mockito internals.

So I don't think it will be fixed anytime soon.

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

GoogleCodeExporter commented 8 years ago
My code example comes from a working project. It doesn't compile on the sun 
compiler because of a bug in the sun compiler concerning generics, try the 
eclipse compiler and it should work.

Concerning the spy issue in my case: The behavior is triggered when the object 
stores a reference to itself in other object (object A) at construction time 
(before the spy is created). When later on the spy access a method of object A 
with 'this' as the argument, the object A will compare the parameter and the 
previous stored 'this'. In production code this will return true because the 
parameter and the previous stored 'this' are the same, with a spy it will 
return false.

The solution imho would be to implement a proxy equals for the spy and spyed-on 
object so that object.equals(spy) and spy.equals(object) return true (after 
being && with any non 'standard' equals implementation of the resp. object).

Original comment by DeRijcke.Erik@gmail.com on 24 Dec 2011 at 3:07

GoogleCodeExporter commented 8 years ago
The current characteristic of the spy is that it is not equal to the spied 
object. It's the same case as for mocks. So it works as designed. See the FAQ - 
mocking equals is not supported.

Hope that helps ;)

Original comment by szcze...@gmail.com on 24 Dec 2011 at 3:59

GoogleCodeExporter commented 8 years ago
Is there any hope that this will change in the future? The equality check in 
our code is in a very crucial and fundamental part of the program. The current 
equal behavior of mockito makes that it is more a burden then a tool for 
testing large parts of our program.

Original comment by DeRijcke.Erik@gmail.com on 25 Dec 2011 at 12:14

GoogleCodeExporter commented 8 years ago
It's unlikely it will change I'm afraid :/

Mockito internal details depend on the equals() method and also changing that 
behavior is a breaking change. There might be users with the exact opposite use 
case and their code depends on the current behavior.

mockito is open source so you can always get sources and taylor it to your 
needs / contribute back ;)

Cheers!

Original comment by szcze...@gmail.com on 25 Dec 2011 at 3:00