InfoQ

News

When To Use Mock Objects?

Posted by Mike Bria on Jun 09, 2008 04:06 AM

Community
Agile
Topics
Unit Testing,
Agile Techniques
Tags
Mocks,
TDD
In his 'Ode To Code' K Scott Allen offers rational for the use of mock objects when unit testing and discusses his thoughts on the use of mock object frameworks.

Allen begins with a sharp argument to what's likely the most common misconception about mocks:
Some people have a misconception that mock objects are only useful if you need to simulate interaction with a resource that is difficult to use in unit tests - like an object that communicates with an SMTP server. This isn't true.
He follows with a reference to Colin Mackay's article on mocks listing some common scenarios mocks are useful:
  • The real object has nondeterministic behavior
  • The real object is difficult to setup
  • The real object has behavior that is hard to trigger
  • The real object is slow
  • The real object is a user interface
  • The real object uses a call back
  • The real object does not yet exist
Allen then gets to the meat of his message when he implies that even the list above may be a tad shortsighted, asserting more generally that "test doubles [mocks] are useful when you want to isolate code under test." In short, according to Allen mocks are to be used to keep the tests for business components independent of all other components the component under test is dependent on; 'A' uses 'B', 'A unit test' should break only if 'A' is broken, regardless of the condition of 'B'.

The article continues with a tie-in to the role of mock objects in true test-driven development:
The authors of "Mock Roles, Not Objects" say that mocks are:

"… a technique for identifying types in a system based on the roles that objects play … In particular, we now understand that the most important benefit of Mock Objects is what we originally called interface discovery".
Allen concludes with a short discussion about the use of mock object frameworks, such as JMock, EasyMock, and NUnit. In summary, his assertion is that mock object frameworks themselves are rather simple although effective use of them can be tricky.

A related discussion worth checking out just surfaced on the TDD Yahoo group, check it out at here.

Related Sponsor

VersionOne is recognized by Agile practitioners as the leader in Agile project management tools. Companies such as Adobe, BBC, CNN, Dow, HP, IBM, Sony and 3M have turned to VersionOne to help deliver greater value to their customers.

4 comments

Reply

Avoiding "redundant" unit test code by Max Weber Posted Jun 9, 2008 11:44 AM
Re: Avoiding by Mike Bria Posted Jun 9, 2008 12:27 PM
Mocks driving design by Danilo Sato Posted Jun 11, 2008 8:12 PM
Link to TDD group by Kenrick Chien Posted Jul 8, 2008 10:01 AM
  1. Back to top

    Avoiding "redundant" unit test code

    Jun 9, 2008 11:44 AM by Max Weber

    In my opinion another advantage of isolating code under test is the avoidance of "redundant" unit test code. According to the above sentence " 'A unit test' should break only if 'A' is broken, regardless of the condition of 'B'. " you don't care whether or not component 'B' works correctly when unit testing component 'A'. If you don't do this kind of isolation through test doubles, you always test a part of the behaviour of component 'B' in the 'A unit test'. So the point is that you already have verified this behaviour in the unit test of component 'B'. Furthermore you have to setup the desired condition of your component 'B' in the test fixture/context of 'A unit test' once again. The redundancy grows even more if you test another component 'C' which also depends on component 'B'. Nevertheless regarding to the "anti composition axiom" you have to test the interaction between these components in form of integration tests once (and only once).

  2. Back to top

    Re: Avoiding

    Jun 9, 2008 12:27 PM by Mike Bria

    Absolutely. Thanks!

  3. Back to top

    Mocks driving design

    Jun 11, 2008 8:12 PM by Danilo Sato

    My reply was getting too long, so I wrote it in my blog:
    http://www.dtsato.com/blog/2008/06/12/using-mocks-to-drive-design/

  4. Back to top

    Link to TDD group

    Jul 8, 2008 10:01 AM by Kenrick Chien

    The link supplied goes to your TDD label in gmail, which I have, by coincidence, the same label in my gmail. :) The actual link is: http://tech.groups.yahoo.com/group/testdrivendevelopment/

Exclusive Content

Measuring Agile in the Enterprise: 5 Success Factors for Large-Scale Agile Adoption

Michael Mah analyzes the development process in 5 companies: 2 Agile (one of them BMC) and 3 classic. He presents the factors which contributed to the success of BMC's Agile adoption.

Tom Preston-Werner on Powerset, GitHub, Ruby and Erlang

In this interview filmed at RubyFringe 2008, Tom Preston-Werner talks about how both Powerset and GitHub use Ruby and Erlang, as well as tools like Fuzed, god, and more.

David Laribee on Alt.NET and its Mission

David Laribee discusses the purpose of ALT.NET, its mission and future.

Discover RailsKits and Stop Writing Redundant Code

Ruby on Rails has become a popular Ruby framework for creating web applications in recent years. An aspect of creating a web application is the need to repeatedly create the same base functionality.

A Formal Performance Tuning Methodology: Wait-Based Tuning

Steven Haines talks about tackling web application performance tuning by proposing a method called wait-based tuning.

Shaw and Fowler About Forging a New Alliance

Shaw and Fowler talk about the need for a new relationship between the business department and the IT department. Studies have shown that projects mostly fail due to miscommunication between the two.

How to GET a Cup of Coffee

In this article, Jim Webber, Savas Parastatidis and Ian Robinson show how to drive an application's flow through the use of hypermedia in a RESTful application.

Archaeopteryx: A Ruby MIDI Generator

Eccentric artist turned overnight anti-celebrity, Giles Bowkett captures the heart and soul of RubyFringe as he demonstrates his revolutionary Archaeopteryx MIDI drum pattern generator.