Mocks are bad… A quick summary

Anders Sveen
2 min readFeb 26, 2020

--

Photo by Frederick Tubiermont on Unsplash

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:

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. :)

--

--

Anders Sveen
Anders Sveen

Written by Anders Sveen

Passionate agile developer and mentor for hire @ Mikill Digital

No responses yet