Skip to content

Mock Objects - taking unit testing and code stubbing to the extreme…

In yesterday’s Developer Network Radio audio chat session with Bob Evans, Bob mentioned that he uses EasyMock for some of his unit testing.  What are Mock Objects?  Who came up with the idea? What is EasyMock? This blog entry will answer these questions and provide links to additional sources of information.

Kent Beck’s book, "Test Driven Development: By Example", presented the notion of writing software that emphasizes automated testing before and during coding. When you do unit testing (using JUnit, DUnit, NUnit, etc) you are often testing the objects and the methods of an object. In most applications it is hard to test code in an isolated way because objects usually interact with (or collaborate with) other objects and parts of whole programs. To unit test a method you’ll often have to stub out external code or simulate other objects and methods.

Mock Objects is a development technique that lets you unit test classes that rely on other classes that don’t exist yet. Mock Objects simulate the object that are required to test pieces of your application. In contrast to stubs, they also verify whether they were used correctly. Tim Mackinnon, Steve Freeman and Philip Craig pioneered the concept of Mock Objects (and coined the term). They first presented Mock Objects at the XP 2000 conference in their paper (PDF format) Endo Testing: Unit Testing with Mock Objects. A reworked version of the paper is published in the book Extreme Programming Examined (Addison-Wesley).

Check out the great paper by Steve Freeman, How Mock Objects Happened.

From the Mock Objects FAQ file, "Mock Objects is based on two key concepts: Replace everything except the code under test with mock implementations that emulate the rest of the environement, and put the test assertions inside those mock implementations so that you can validate the interactions between the objects in your test case."

Another useful article (PDF Format) that gives an overview of Mock Objects and more importantly covers several Mock Object tools is the paper, Making a Mockery" by Ivan Moore and Sebastian Palmer.

Bob Evans mentioned in his audio chat that he uses the tool EasyMock. EasyMock - EasyMock provides Mock Objects for interfaces in JUnit tests by generating them on the fly using Java’s proxy mechanism.

Mock Objects are also covered on the c2.com wiki.

{ 4 } Comments

  1. David Hervieux | December 6, 2004 at 6:37 pm | Permalink

    For .Net, take a look at POCMock. It’s really powerful. You can Mock anything:

    http://fr.www.prettyobjects.com/POCMock.aspx?sid=Features

    – dhervieux

  2. sdf | December 10, 2004 at 1:03 am | Permalink

    sfd

  3. shetrman | June 24, 2005 at 4:58 am | Permalink

    aldsiasjdlak

  4. Bruce | May 18, 2006 at 1:49 pm | Permalink

    Mock objects break unit test integrity. I’ve seen great Software Engineers abuse mocking frameworks by mocking everything thereby forcing a test to pass. I don’t believe in fake or mocked objects. Just test the system as a whole if you have to. Yeah, even the interactions of objects. Why not? Just use the real objects. If you find it to difficult to set up real objects then your design needs to be reannalized and simplified. Another thing I’ve seen seasoned Software Engineers do is set values that are improbable of occuring and testing against those erroneous values.

Post a Comment

Your email is never published nor shared. Required fields are marked *

Bad Behavior has blocked 1553 access attempts in the last 7 days.

Close