Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger


Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I've seen the following terms tossed around: mock, dummy, fake, stub. Do you think it is useful to delineate between these and, if so, how would you describe the differences?
Posts: 40
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

the situation is as following. Few years ago, Gerard Meszaros did a fantastic things of putting up a coherent nomenclature in his book "xUnit Test Patterns". Some of the terms are now widely used but not always coherently, and not always with the same meaning that Meszaros defined (personally I feel that we, as a community, have lost our chance to make our communication clear by misusing and overusing many of them).

You ask about four types of so-called test-doubles, which is a generic name for all types of objects which pretend to be real collaborators of a class (of a SUT, to be more precise). The definitions are as follows (I have them described in the book, but I will come up with ad-hoc definitions here):
* dummy - not used but has to be there (it is required so the code compiles and runs but its functionality is not used during tests) - do not worry about it, you will rarely need it,
* fake - pretends to be a someone else but is less powerful (e.g. in-memory database instead of full-blown database installation, but good enough for at least some kind of tests)
* stub - provides data to tested class (to a SUT) - its task is to pass
* mock - used to verify if some methods were called on collaborating objects (verification of indirect outputs); the term "mock" is also commonly used as an umbrella name for all test-doubles (which brings confusion, but I think this battle is already lost...)
If you dig some more you will also encounter saboteur (a specialized stub which throws all kinds of exceptions thus allowing to test many specific errors-related scenarios), or a test-spy (with at least two meanings - one which is almost identical with mock, and the other being a name of a special technique of substituting only selected methods of mocked object) and probably some more.

When it comes to distinguishing between these various things, I think it is crucial to understand the difference between mock and stub. This is because they play very different role in tests, and confusing them may lead to overspecified tests. What is important is that we verify behaviour of mocks (or more precisely, we verify if tested class calls specific methods of mock) but we do not do that with stubs. We expect stubs to provide us some values, but we do not really care if they do it or not (we do not verify if tested class calls specific methods of stubs). This allows for writing very focused tests, where only specific scenario is being verified, and results in tests having minimal coupling with implementation (however, tests which use test-doubles are always glued to implementation!).
Paul Croarkin
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Tomek. It is useful to think in these terms, even if most people are going to use the blanket term 'mock' for all of them.

My guess is that, using your definitions, there are more 'fakes' and 'stubs' out there in the wild than there are 'mocks' even though most people probably call them 'mocks'. I hadn't heard the term 'saboteur' before, but I do use them frequently.


No, tomorrow we rule the world! With this tiny ad:
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic