Summary & Quiz: Test Doubles – Stubs and Mocks

Test yourself on what you have learned so far.

We'll cover the following

Section’s recap

In this section, we’ve looked at how to solve the problem of testing problematic collaborators. We have 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 double are especially useful to us: the stub and the mock. Stubs return data. Mocksverify 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 section, we are 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.