The mocking story continues…

December 14, 2008

In our most recent project, we are using Mockito as the mocking framework of choice, instead of EasyMock which we’ve been using for quite a while. Differences between the two frameworks are enunciated on the Mockito site. The things that appeal most to me are:

  • I don’t have to record void methods in the mock, unless I want the mock to throw an exception.
  • I don’t have to invoke replay() for every mock!
  • The really big thing for me is that I can invoke verify() selectively, on those method invocations that I care about and not any others.

This last point, which has been the focus of past rants, was illustrated very well by Dan North at his BDD session at Agile2008. Loosely paraphrasing, his example went thus:

Imagine you want to play a trick on someone. You wait for him to pull in at a gas station, fill up gas and walk inside to pay for the gas, then use a bulldozer to destroy his car. You are interested in observing his reactions to the destruction of his car. Everybody at the gas station – the other customers, the person behind the counter – are all actors (mocks). To determine if you predicted his reactions accurately, you don’t want to query all the actors in this little play; you don’t really care what the bulldozer driver has to say regarding this. You are mainly interested in the two people closest to the subject at the time – the person behind the counter and the person directly behind him in line. And you are largely uninterested in the rest of their observations – only in their specific observations to his reactions.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: