BT

InfoQ Homepage News Mockito 1.5 spies on plain objects

Mockito 1.5 spies on plain objects

Bookmarks

Mockito is a mocking framework for Java. It's very similar to EasyMock and jMock, but eliminates the need for expectations by verifying what has been called after execution. Other mocking libraries require you to record expectations before execution, which tends to result in ugly setup code.

Lack of expectations is a big win, as software developer Hamlet D'Arcy, a Mockito user, explains on his blog:

"Mockito really cleans up the unit test by not requiring expectations. Personally, I much prefer the Mockito API to the EasyMock API for that reason."

Ketan Padegaonkar at ThoughtWorks lavishes praise on Mockito's unique syntax too:

"This looks, and reads far better than the JMock syntax, and it was love at first sight; I'm test-infested now."

The 1.5 release of Mockito introduces the ability to "spy" on non-mock objects by verifying that certain methods get called on them. For example:

List list = new LinkedList();
List spy = spy(list);

Our "spy" variable now acts as a handle to the list object. It can be treated just like a mock object. For example, the size method can be stubbed out:

stub(spy.size()).toReturn(100); 

Normal methods can also be called and verified:

spy.add("one");
verify(spy).add("one");

Another improvement is an alternate, more readable stubbing syntax. For example:

stub(obj.someMethod()).toReturn(result);

Can now be written as:

doReturn(result).when(obj).someMethod(); 

This is the same length as the old style, but reads more cleanly and has fewer parenthesis. In addition to doReturn(), there are also doThrow(), doAnswer(), and doNothing() expectations.

Other useful features of Mockito include:

  • Ability to mock concrete classes and interfaces
  • Custom argument matchers
  • Ordered verification
  • Clean stack traces

Getting started with Mockito is as simple as adding a JAR file to your classpath. Visit the Google Code project for installation instructions, documentation, and more code samples.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Java has it all :(

    by craig w /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I'm excited to try this out.

    Just wish Flex/AS3 had something as nice.

  • It's good to see proper support for Test Spies

    by Gerard Meszaros /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    For more information on the difference between Mock Objects and Test Spies (a form of Test Stub), refer to "xUnit Test Patterns - Refactoring Test Code" by Gerard Meszaros in hard-copy, Safari, or via the draft version available at xunitpatterns.com .

    Best,

    Gerard

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.