Testing Without Mocks
May 26, 2020
“Testing Without Mocks” TrainingJun62023
I’m offering live online training for this material in four 3-hour sessions from June 6th to 15th. Register here.
To be notified about future “Testing Without Mocks” training courses, join the mailing list here (requires Google login).
For private training, contact me directly.
Each week in my Tuesday Lunch & Learn livestream, we look at a useful technical skill, define a challenge related to that skill, and solve the challenge live. This week, it’s testing without mocks.
Although mocks (and spies) are useful for testing interactions and isolating code, that isolation comes at a cost. Mocks can only check if dependencies’ methods are being called. They can’t check if they’re being called correctly. As a result, mock-based tests must be supplemented with integration tests, and these tests are slow and expensive. Wouldn’t it be nice if we could write unit tests that didn’t need additional integration testing?
Our challenge this week: given a simple command-line application, update the existing mock-based tests so they work without mocks, spies, or integration tests. The end result is surprisingly effective: tests that are more reliable than mock-based tests, faster, and easier to read.
To follow along, download the code from GitHub and check out the
2020-05-26 tag. To see the final result, check out the
2020-05-26-end tag or view it on GitHub.
For more details about testing without mocks, see the Testing Without Mocks Pattern Language.
Visit the Lunch & Learn archive for more.