Exploring Mock Objects in Testing
Explore how mock objects function as test doubles to record interactions in Java unit tests. Understand their role in verifying method calls, ensuring that your system under test communicates correctly with its collaborators. This lesson guides you through implementing mock objects using dependency inversion, helping you write reliable tests without relying on external systems like mail servers.
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 ...