Exploring Mock Objects in Testing
Understand how mock objects are used to record interactions between an 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’ll see in this section.
Understanding mock objects
Mock objects are a kind of test doubles that record interactions. Unlike a stub, which supplies well-known objects to the SUT, a mock will simply record interactions that the SUT has with the mock. It’s 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.