background preloader

Mock Objects

Facebook Twitter

Mockobjects. Mocks Aren't Stubs. The term 'Mock Objects' has become a popular one to describe special case objects that mimic real objects for testing.

Mocks Aren't Stubs

Most language environments now have frameworks that make it easy to create mock objects. What's often not realized, however, is that mock objects are but one form of special case test object, one that enables a different style of testing. In this article I'll explain how mock objects work, how they encourage testing based on behavior verification, and how the community around them uses them to develop a different style of testing. I first came across the term "mock object" a few years ago in the XP community. Since then I've run into mock objects more and more. But as often as not I see mock objects described poorly. This difference is actually two separate differences. (In the earlier version of this essay I had realized there was a difference, but combined the two differences together.

Regular Tests I'll begin by illustrating the two styles with a simple example. jMock - An Expressive Mock Object Library for Java. Mock object. In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways.

Mock object

A programmer typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to simulate the dynamic behavior of a human in vehicle impacts. Reasons for use[edit] In a unit test, mock objects can simulate the behavior of complex, real objects and are therefore useful when a real object is impractical or impossible to incorporate into a unit test. If an actual object has any of the following characteristics, it may be useful to use a mock object in its place. If the real object: For example, an alarm clock program which causes a bell to ring at a certain time might get the current time from the outside world. Technical details[edit] Mocks, fakes and stubs[edit] Setting expectations[edit] Consider an example where an authorization sub-system has been mocked. Writing log strings[edit]

Unit testing with mock objects. Unit testing has become widely accepted as a "best practice" for software development.

Unit testing with mock objects

When you write an object, you must also provide an automated test class containing methods that put the object through its paces, calling its various public methods with various parameters and making sure that the values returned are appropriate. When you're dealing with simple data or service objects, writing unit tests is straightforward. However, many objects rely on other objects or layers of infrastructure.

When it comes to testing these objects, it is often expensive, impractical, or inefficient to instantiate these collaborators. For example, to unit test an object that uses a database, it may be burdensome to install, configure, and seed a local copy of the database, run your tests, then tear the local database down again. Mock in the middle The common coding style for testing with mock objects is to: The heart of the problem is that the object is obtaining these objects "on the inside. " Roles. Mock Objects. EasyMock : Home.