Exploring Mock Objects in Testing

Understand how Mock objects are used to record interactions between SUT and its collaborators, allowing verification of method calls and parameters passed.

In this lesson, we’ll take a look at another important kind of test double: the mock object. Mock objects solve a slightly different problem than stub objects do, as we shall see in this section.

Understanding mock objects

Mock objects are a kind of test double that record interactions. Unlike stubs, which supply well-known objects to the SUT, a mock will simply record interactions that the SUT has with the mock. It is the perfect tool to answer the question,

“Did the SUT call the method correctly?”

This solves the problem of push model interactions between the SUT and its collaborator. The SUT commands the collaborator to do something rather than requesting something from it. A mock provides a way to verify that it issued that command, along with any necessary parameters. The following UML object diagram shows the general arrangement:

Get hands-on with 1200+ tech skills courses.