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.

When To Use Mock Objects?

Posted by Mike Bria on Jun 09, 2008

Sections
Process & Practices,
Development
Topics
Agile ,
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.
Avoiding "redundant" unit test code by Max Weber Posted
Re: Avoiding by Mike Bria Posted
Mocks driving design by Danilo Sato Posted
Link to TDD group by Kenrick Chien Posted
  1. Back to top

    Avoiding "redundant" unit test code

    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

    by Mike Bria

    Absolutely. Thanks!

  3. Back to top

    Mocks driving design

    by Danilo Sato

    My reply was getting too long, so I wrote it in my blog:


    www.dtsato.com/blog/2008/06/12/using-mocks-to-d...

  4. Back to top

    Link to TDD group

    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: tech.groups.yahoo.com/group/testdrivendevelopment/

Educational Content

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.

Architecting Visa for Massive Scale and Continuous Innovation

John Davies examines Visa’s architecture and shows how enterprises have architected complex integrations incorporating Hadoop, memcached, Ruby on Rails, and others to deliver innovative solutions.

Max Protect: Scalability and Caching at ESPN.com

Sean Comerford unveils ESPN.com’s architecture, what components are used and why, and the current changes the website goes through.

The Seven Deadly Sins of Enterprise Agile Adoption

Are there repeated patterns of failure on Enterprise Agile Enablement efforts? Sanjiv and Arlen discuss Seven Deadly Sins to avoid when adopting Agile in an enterprise.

Questions for an Enterprise Architect

Erik Dörnenburg answers: What is Enterprise and Evolutionary Architecture?, discussing 4 issues: Turning strategy into execution, Ensuring conformance, Where do the architects sit? Buying or building?

Wrap Your SQL Head Around Riak MapReduce

Sean Cribbs explains what Map-Reduce and Riak are, why and how to use Map-Reduce with Riak, and how to convert SQL queries into their Map-Reduce equivalents.