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

Posted by on in Blogs
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.



About
Gold User, Rank: 1, Points: 2466
David Intersimone (known to many as David I.) is a passionate and innovative software industry veteran-often referred to as a developer icon-who extols and educates the world on Embarcadero developer tools. He shares his visions as an active member of the industry speaking circuit and is tapped as an expert source by the media. He is a long-standing champion of architects, developers and database professionals and works to ensure that their needs are folded into Embarcadero's strategic product plans. David holds a bachelor's degree in computer science from California Polytechnic State University at San Luis Obispo, California.

Comments

  • Guest
    David Hervieux Monday, 6 December 2004

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



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



    -- dhervieux

  • Guest
    sdf Thursday, 9 December 2004

    sfd

  • Guest
    shetrman Thursday, 23 June 2005

    aldsiasjdlak

  • Guest
    Bruce Thursday, 18 May 2006

    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.

  • Guest
    silver Sunday, 21 December 2008

    Easy Mock is used only for SIP testing ???

    Then what tool can we use for IN and CAP protocol testing...

  • Guest
    silver Sunday, 21 December 2008

    Easy Mockis only used for SIP testing ???

    Then what tool we can use for IN and CAP protocol Testing ??

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: