zero interaction mocks

638 views
Skip to first unread message

Jay Fields

unread,
Dec 26, 2008, 10:57:37 AM12/26/08
to mockito
In my tests, I've been working with the following descriptions:
http://www.martinfowler.com/bliki/TestDouble.html

Obviously, Mockito fills the mock need. It also fills the stub need
(for me) because it's canned answers are what I'm looking for.

I'm not looking for it to fill the fake need.

However, I was wondering if there's a way to make it fill the dummy
need. I've taken Martin's definition a bit strictly, in that I want to
know if my Dummy objects are used in any way.

On some occasions I use verifyZeroInteractions and everything works
nicely; however, on other occasions I don't want to verify. Instead
I'd like an option where I can specify that if any method is called on
a mock an exception will be thrown.

I checked the docs and didn't see anything. Is this possible?

Cheers, Jay

szczepiq

unread,
Dec 26, 2008, 12:21:22 PM12/26/08
to moc...@googlegroups.com
Hi,

It's an interesting case. I guess what you're looking for is a classic
mock - it throws exception unless the interaction is 'expected'.
Mockito does not support it directly but here are your options:

1).
...
exercise();
...
verifyZeroInteractions(mock);

It's not perfect. You might end up with NPEs when null return values
from mocks are processed.

2).
Use the new feature of Mockito, e.g. changing the default return
values for mocks. This feature is yet in trunk but I'll try to release
it this Christmas/New Year break.

mock(Foo.class, ALWAYS_THROWS_EXCEPTIONS);

ALWAYS_THROWS_EXCEPTIONS is your implementation of ReturnValues interface


3).
Figure out a new, better way of doing it and show us... :)

Cheers,
Szczepan Faber

Jay Fields

unread,
Dec 26, 2008, 12:34:21 PM12/26/08
to moc...@googlegroups.com
Hi Szczepan,

The second option looks good. I'll probably also create a method 'dummy' that I can static import and it'll do the other stuff behind the scenes. Maybe that would make sense in Mockito?

Either way, thanks for the help.

Cheers, Jay

szczepiq

unread,
Dec 26, 2008, 1:33:33 PM12/26/08
to moc...@googlegroups.com
>I'll probably also create a method 'dummy'
> that I can static import and it'll do the other stuff behind the scenes.
> Maybe that would make sense in Mockito?

Cool, we'll think about it. I'm always a bit reluctant to expand the
API but maybe your case deserves it :)

Thanks!
Szczepan

szczepiq

unread,
Dec 28, 2008, 9:54:53 AM12/28/08
to moc...@googlegroups.com
Jay, are you there yet?

>On some occasions I use verifyZeroInteractions and everything works
>nicely; however, on other occasions I don't want to verify. Instead
>I'd like an option where I can specify that if any method is called on
>a mock an exception will be thrown.

Can you tell me more about your use case? I was thinking about it
lately and I guess I'd prefer explicit verifyZeroInteractions() to
implicit dummy. Main reason is that if <I don't want any method called
on a mock> then this fact is my *assertion* and it feels to belong to
the 'assert' part of the test rather than to the 'setup' part where
the dummy is created. Does your need for a dummy originate from
NullPointerExceptions when the code works on return values from mocks?

Cheers,
Szczepan Faber

Jay Fields

unread,
Dec 28, 2008, 10:51:16 AM12/28/08
to moc...@googlegroups.com
Hi Szczepan,

After some thought, I don't think you should include dummy in mockito
either. I think the idea might be sound, but it's unproven. Until it's
used often and successfully it probably doesn't belong.

As far as use cases go, I don't have any good concrete examples. I was
striving to come up with a concrete implementation for each of the
ideas from the post I originally linked to. However, I currently have
zero dummies in my tests. I'm not sure if that's because I don't have
the ability to easily create then or if it's because they are
unnecessary.

I do have zeroInteraction assertions, but as you said, those are cases
where that's the primary focus of the test and the assertion is
perfectly suited for the task.

I am working with very low-latency systems these days, so conceptually
each method call does matter. Therefore, there is a possibility of
middle ground where I don't want to always assert that no interactions
took place, but I do want to be alerted if additional unexpected
interactions take place. Sort of like a warning that unexpected
interactions took place. . However, I haven't really focused on this
approach, so I don't have good practical experience.

Once I get the ability to easily create dummies I'll add a few to my
code base and let you know what the results are.

Cheers, Jay

Sent from my iPhone

szczepiq

unread,
Dec 28, 2008, 10:59:22 AM12/28/08
to moc...@googlegroups.com
Ok, Thanks!
Szczepan
Reply all
Reply to author
Forward
0 new messages