"Mockito spies are not partial mocks. Mockito spy is meant to help
testing other classes - not the spy itself. Therefore spy will not
help if you intend to verify if method calls other method on the same
object."
It should be fine if you try spy.method_to_be_mock(), but
spy.method_to_be_test() will invoke method on the original instance.
That's why you have exception there.
Regards,
Bartosz Bankowski
I don't like partial mocks because they seem to deliver a wrong
message. Traditional mocks say: "hey, this logic belongs to a
different object, go ahead and create new interface/class so you can
mock it". Partial mocks seem to say: "hey, don't worry about SRP
(single responsibility principle) and leave the logic in the same
class. Create a new method so you can partial-mock it".
Also, partial mocks make my test dependent on the *invocations between
internal methods* of the class. This limits refactoring and makes my
test focused on less important implementation details instead of
focused on interesting behavior. I'm fine when the test depends on the
*invocations on dependencies*. Why? Well, because it's a sole of
object orientation to me: objects delegating resposibilities to other
objects.
This is of course my POV, written late night so I'm not 100% sure it
all makes sense... There are probably hundreds of happy
partial-mockers out there so don't worry too much if you disagree with
me. There are also older threads on this list with smarter opinions on
partial mocking.
Spying in mockito is a bit unfortunate feature because it leads to the
questions you raised. Also, I bet there are people cursing mockito
"darn spying doesn't work!" but they are not so OS-enabled to send an
email to our list. I guess mockito might evolve into partial mocking
(with a dear hope that people will use it occasionaly and
judicisiosly).
Cheers,
Szczepan Faber
2008/11/22 Jarod Liu <liuyu...@gmail.com>: