mensahs / mockito

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

How to spy private method #490

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Now I want to spy private method by java reflect, but when I run my code, it 
tells me my spy object has not the private method, what should I do?

Object spyObject = spy(object);
setPrivateMethodAccessible(spyObject, "updateComment", parameters);
return spyObject;

Original issue reported on code.google.com by xierui.s...@gmail.com on 18 Apr 2014 at 8:55

GoogleCodeExporter commented 8 years ago
private methods are only declared in the spied type, when Mockito spy an 
object, it extends that type, hence it cannot see or access that private method.

Mocking private methods is test smell, a design smell, and not Object Oriented. 
The code should be refactored to enable the test to alter the behavior of the 
collaborating object (the one that replace the private method).

Original comment by brice.du...@gmail.com on 21 Apr 2014 at 5:24