InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Mockito 1.5 spies on plain objects

Posted by Matthew Bass on Sep 18, 2008

Sections
Development
Topics
Unit Testing ,
Java
Tags
Testing ,
Mocks ,
TDD

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.

Java has it all :( by craig w Posted
It's good to see proper support for Test Spies by Gerard Meszaros Posted
  1. Back to top

    Java has it all :(

    by craig w

    I'm excited to try this out.

    Just wish Flex/AS3 had something as nice.

  2. Back to top

    It's good to see proper support for Test Spies

    by Gerard Meszaros

    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

Educational Content

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.

Beauty Is in the Eye of the Beholder

Alex Papadimoulis discusses ugly code, where it comes from, how to avoid it, and how to get rid of it.