Mocks are bad… A quick summary
While reading about the different Kotlin dependency injection frameworks; I was reminded how mocking was one of the reasons people wanted to do DI frameworks back in the days. It is even advertised as a feature in some frameworks still.
We do not use a DI framework ourselves (it is easy to just inject everything yourself). But even if we did, we definitely do not want to use mocks. We actually have no mocks in our codebase. Fakes are easy to create and much, much better.
Mocks test internal behavior as opposed to verifying the results of operations. And they are hard to re-use across tests.
This will hit you hard when you try to change and re-factor your system, as you will have to fix the same thing in many tests.
I could go on, but this has actually been quite extensively covered. So I will settle for sharing some good resources:
- Mocks aren’t stubs — Martin Fowler (Java)
- Unit test — Martin Fowler
- Mocking is a code smell — Eric Elliott (JavaScript)
- When writing unit tests, don’t use mocks — Seth Ammons (Go)
- Careless mocking considered harmful — Philippe Bourgau (Ruby)
And here is a really good article showing the benefits of Fakes (thanks to Asgaut Mjølne):
https://medium.com/@june.pravin/mocking-is-not-practical-use-fakes-e30cc6eaaf4e
I plan to write some about how we do fakes and dependency injection easily in Kotlin later. Stay tuned. :)