Mocks, stubs, fakes, and spies – commonalities and differences

Before digging deeper into the concepts behind unit testing, we need to discuss and disambiguate some of the terms that we will be using in the upcoming sections. While these terms have been out there for years, software engineers tend to occasionally conflate them when writing tests. A great example of such confusion becomes evident when you hear engineers use the terms mock and stub interchangeably.

To establish some common ground for a fruitful discussion and to clear any confusion around this terminology, let's examine the definition of each term, as outlined by Gerard Meszaros [5] in his XUnit Test Patterns: Refactoring Test Code book on test patterns.