Summary and Quiz: Test Doubles—Stubs and Mocks

Test your knowledge of test doubles including stubs and mocks.

We'll cover the following

Chapter’s recap

In this chapter, we’ve looked at how to solve the problem of testing problematic collaborators. We’ve learned how to use stand-in objects for collaborators called test doubles. We’ve learned that this gives us simple control over what those collaborators do inside our test code. Two kinds of test doubles are especially useful to us: the stubs and the mocks. Stubs return data. Mocks verify that methods were called. We’ve learned how to use the Mockito library to create stubs and mocks for us. We’ve used AssertJ to verify the SUT behaved correctly under the various conditions of our test doubles.

We’ve learned how to test error conditions that throw exceptions. These techniques have expanded our toolkit for writing tests. In the next chapter, we’re going to cover a very useful system design technique that allows us to get most of our code under FIRST unit test, and at the same time avoid the problems of testing collaborations with external systems that we cannot control.

Quiz

Get hands-on with 1200+ tech skills courses.