Mock Objects. Scary. Not really. It’s actually a much
simpler concept than you think. Just follow my lead as you read this chapter.
But first, a recap.
In the previous chapter we solved the problem of our code under
test depending on other objects to run correctly. We used stubs to make
sure that the code under test got everything it needed so that we could test
its logic independently of any other logic it relies upon.
In this chapter we will attempt to solve a bigger issue; how do
you test that an object simply calls other objects correctly?It may not return any result, or save any
state, but it has complex logic which results in correct calls to other objects
to do the correct work. How do you test that your object indeed interacts
with other objects correctly? That’s the subject of this chapter – we’ll use Mock
Objects.
Before we continue though, let’s try to define what Interaction
Testing is, and how it’s different from the testing we’ve done so far – State-Based testing.
Interaction Testing: Definition
Interaction Testing is the act of testing how an object under
test calls and receives input from other objects.
State-Based Testing: Definition
Also called “State Verification”, we determine whether the
exercised method worked correctly by examining the state of the system under
test and its collaborators (dependencies) after the method was exercised.
Now I will show you how we can use them in our unit tests.