What ‘Preferring Fakes Over Mocks’ Truly Means?

Revisiting Stubs, Fakes, and Mocks Again

Ryan W
12 min readSep 6, 2024

--

This is the second attempt I have discussed the same topic after attending a sharing session about testing that quoted Martin Flower’s definition of stubs, fakes and mocks, then demonstrated something that did not truly adhere to the definitions.

This image was created using an AI image creation program.

Over the years, there’s been an increasing trend in the Android development community to say they “prefer fakes over mocks” when writing tests, especially in unit testing. This trend has led to confusion, as many developers replace mocking libraries like Mockito or MockK with manually written classes without truly understanding the underlying concepts of stubs, fakes, and mocks.

While it may seem that switching from mocking libraries to manual classes is an improvement, it’s important to realise that these two practices can still lead to a misunderstanding of what test doubles (like fakes and mocks) really mean and how they should be used. What’s truly at play is a deeper debate about test design, the role of behaviour verification versus state verification, and when to use fakes, mocks, or stubs appropriately.

Martin Fowler’s Definitions

To clear up this confusion, let’s start with Martin Fowler’s definitions from his famous article…

--

--

Ryan W
Ryan W

Written by Ryan W

Modern Android Development | Jetpack Compose | Kotlin Multiplatform 📱Creating Android Solutions From Concept to Product | Building Apps That Matter Since 2010

Responses (1)