What is Object Mocking and when do I need it?

Many people use Mock Objects when they are writing unit tests. What is a Mock Object? Why would I ever need one? Do I need a Mock Object Framework?


Solution 1:

Object Mocking is used to keep dependencies out of your unit test. Sometimes you'll have a test like "SelectPerson" which will select a person from the database and return a Person object.

To do this, you would normally need a dependency on the database, however with object mocking you can simulate the interaction with the database with a mock framework, so it might return a dataset which looks like one returned from the database and you can then test your code to ensure that it handles translating a dataset to a person object, rather than using it to test that a connection to the database exists.

Solution 2:

Several people have already answered the 'what', but here are a couple of quick 'whys' that I can think of:

  1. Performance

    Because unit tests should be fast, testing a component that interacts with a network, a database, or other time-intensive resource does not need to pay the penalty if it's done using mock objects. The savings add up quickly.

  2. Collaboration

    If you are writing a nicely encapsulated piece of code that needs to interact with someone else's code (that hasn't been written yet, or is in being developed in parallel - a common scenario), you can exercise your code with mock objects once an interface has been agreed upon. Otherwise your code may not begin to be tested until the other component is finished.

Solution 3:

A mock object lets you test against just what you are writing, and abstract details such as accessing a resource (disk, a network service, etc). The mock then lets you pretend to be that external resource, or class or whatever.

You don't really need a mock object framework, just extend the class of the functionality you don't want to worry about in your test and make sure the class you are testing can use your mock instead of the real thing (pass it in via a constructor or setter or something.

Practice will show when mocks are helpful and when they aren't.

EDIT: Mocking resources is especially important so you don't have to rely on them to exist during the test, and you can mock the details of how they exist and what they respond (such as simulating a FileNotFoundException, or a webservice that is missing, or various possible return values of a webservice)... all without the slow access times involved (mocking will prove MUCH faster than accessing such resources in the test).

Solution 4:

Do I need a Mock Object Framework?

Certainly not. Sometimes, writing mocks by hand can be quite tedious. But for simple things, it's not bad at all. Applying the principle of Last Responsible Moment to mocking frameworks, you should only switch from hand-written mocks to a framework when you've proven to yourself that hand-writing mocks is more trouble than it's worth.

If you're just getting starting with mocking, jumping straight into a framework is going to at least double your learning curve (can you double a curve?). Mocking frameworks will make much more sense when you've spent a few projects writing mocks by hand.

Solution 5:

Object Mocking is a way to create a "virtual" or mocked object from an interface, abstract class, or class with virtual methods. It allows you to sort of wrap one of these in your own definition for testing purposes. It is useful for making an object that is relied on for a certain code block your are testing.

A popular one that I like to use is called Moq, but there are many others like RhinoMock and numerous ones that I don't know about.