# 30 Days of Automation in Testing Day 11- Compare and contrast Mocking,Stubbing and Faking

**URL:** <https://club.ministryoftesting.com/t/30-days-of-automation-in-testing-day-11-compare-and-contrast-mocking-stubbing-and-faking/16699>\
**Category:** 30 Days of Testing\
**Tags:** 30-days-of-testing, automation\
**Created:** [12 July 2018 04:02 UTC](https://club.ministryoftesting.com/t/30-days-of-automation-in-testing-day-11-compare-and-contrast-mocking-stubbing-and-faking/16699 "2018-07-12T04:02:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![celinetester](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@celinetester](https://club.ministryoftesting.com/u/celinetester)\
**Post date:** [12 July 2018 04:02 UTC](https://club.ministryoftesting.com/t/30-days-of-automation-in-testing-day-11-compare-and-contrast-mocking-stubbing-and-faking/16699/1 "2018-07-12T04:02:09Z")

</div>

#30daytestingchallenge  
#day11of 30daytestingchallenge

As somebody relatively new and starting out in test automation, I tried to search for a beginner-friendly explanation of concepts of what on earth is stubbing, mocking and faking. Most definitions are technical as it is used in unit testing or test driven development (TDD).

**If anyone has a better example or clearer understanding, please contribute to this thread.**

Thank you.

From what I’ve gathered:

- Test Double as the generic term for any kind of pretend object used in place of a real object for testing purposes. The name comes from the notion of a Stunt Double in movies.

- Mocks and stubs are techniques that are used at the boundaries of the code under test.

- Mocks are objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

- There is a difference in that the stub uses state verification while the mock uses behaviour verification.

- Fake objects actually have working implementations, but usually, take some shortcut which makes them not suitable for production (an in-memory database is a good example).

Sources:

[Mocks Aren’t Stubs](https://martinfowler.com/articles/mocksArentStubs.html#TheDifferenceBetweenMocksAndStubs)

[Test Doubles](http://testing-for-beginners.rubymonstas.org/test_doubles.html)

---

<div class="post-metadata">

**Author:** ![dkotschessa](https://avatars.discourse-cdn.com/v4/letter/d/fbc32d/32.png) [@dkotschessa](https://club.ministryoftesting.com/u/dkotschessa)\
**Post date:** [12 July 2018 17:36 UTC](https://club.ministryoftesting.com/t/30-days-of-automation-in-testing-day-11-compare-and-contrast-mocking-stubbing-and-faking/16699/2 "2018-07-12T17:36:35Z")

</div>

That was a great start @celinetester. Everything I search for takes me to the same article. I’ve been perusing it for awhile, re-reading a bit, trying to understand.

You say:

> - Mocks are objects pre-programmed with expectations which form a specification of the calls they are expected to receive.
> 
> - There is a difference in that the stub uses state verification while the mock uses behaviour verification.

So I realized I didn’t know the difference between state verification and behavior verification. So I came across this article, appropriately titled [State vs Behaviour Verification](https://manas.tech/blog/2009/04/30/state-vs-behaviour-verification.html), by Santiago Palladino, which also refers back to the same article by Martin Fowler we started with.

He says:

> In state verification you have the object under testing perform a certain operation, after being supplied with all necessary collaborators. When it ends, you examine the state of the object and/or the collaborators, and verify it is the expected one.
> 
> In behaviour verification, on the other hand, you specify exactly which methods are to be invoked on the collaborators by the SUT, thus verifying not that the ending state is correct, but that the sequence of steps performed was correct.

He gives an example with a User Mail notifier service (the system under test) which pulls from a user repository and sends mail using a Mail Service.

The example includes some code and I am not 100% there yet in trying to decipher the difference, so I think this will take me more reading to understand. I also found an article [Why I Don’t Like Mocks](http://www.draconianoverlord.com/2010/07/09/why-i-dont-like-mocks.html), and what I can gather about state vs. behavior testing is that the former is more general, and possibly less brittle.

I would love to see a non-code example, i.e. an IRL example of testing a piece of hardware, an object, a dinner plate…I don’t care. Just something I could get my head around!

-Dave K

---

<div class="post-metadata">

**Author:** ![celinetester](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@celinetester](https://club.ministryoftesting.com/u/celinetester)\
**Post date:** [13 July 2018 01:25 UTC](https://club.ministryoftesting.com/t/30-days-of-automation-in-testing-day-11-compare-and-contrast-mocking-stubbing-and-faking/16699/3 "2018-07-13T01:25:42Z")

</div>

Exactly. I was also searching for non-code and simple worded examples. I came across a coffee-making analogy in Youtube but still having it explained in source code makes it more intimidating. Too advanced for me!

---

<div class="post-metadata">

**Author:** ![vic](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@vic](https://club.ministryoftesting.com/u/vic)\
**Post date:** [13 July 2018 07:50 UTC](https://club.ministryoftesting.com/t/30-days-of-automation-in-testing-day-11-compare-and-contrast-mocking-stubbing-and-faking/16699/4 "2018-07-13T07:50:56Z")

</div>

#30daytestingchallenge  
#day11of 30daytestingchallenge

Fake  
A fake is a class that you can use instead of actual line of business code

Stub  
Stubs are objects “that provide canned answers to calls made during the test.” This might seem the same as the fake but the biggest difference is that a mocking framework like JustMock can be used to create the stub in the test, providing the necessary scaffolding for the system under test in very little code.

Mock describes special objects that mimic real objects for testing  
Mock acts as collaborators and are subject to behavioral modification. These collaborators will help you achieve your desired unit-testing goal for a given system and a controlled set of inputs. This is achieved by having the mock object stand in for the real object and returning data you have specified.  
Mock brings all of the benefits of stubs plus the ability to specify behavior

A good source that gives particular examples to explain about these definition:

> **[Fakes, Stubs, and Mocks](https://www.telerik.com/blogs/fakes-stubs-and-mocks)**
>
> In Doing Your First Mock, Mehfuz Hossain explained why you should use mocking to improve your unit tests and then used a real world example to demonstrate this

---

<div class="post-metadata">

**Author:** ![heather\_reid](https://sea2.discourse-cdn.com/flex020/user_avatar/club.ministryoftesting.com/heather_reid/32/29_2.png) [@heather\_reid](https://club.ministryoftesting.com/u/heather_reid)\
**Post date:** [13 July 2018 11:17 UTC](https://club.ministryoftesting.com/t/30-days-of-automation-in-testing-day-11-compare-and-contrast-mocking-stubbing-and-faking/16699/5 "2018-07-13T11:17:57Z")

</div>

Grabbing some tweets for this:

> <https://twitter.com/PeetRonics/status/1016979871254368257>

> <https://twitter.com/DolinskiAndrzej/status/1017163700493520896>

> <https://twitter.com/beloquintana/status/1017231564420329474>

> <https://twitter.com/beloquintana/status/1017232378790596614>

> <https://twitter.com/Toinedotcom/status/1017318318112432128>

---

<div class="post-metadata">

**Author:** ![qakumarnz](https://sea2.discourse-cdn.com/flex020/user_avatar/club.ministryoftesting.com/qakumarnz/32/3434_2.png) [@qakumarnz](https://club.ministryoftesting.com/u/qakumarnz)\
**Post date:** [15 July 2018 21:29 UTC](https://club.ministryoftesting.com/t/30-days-of-automation-in-testing-day-11-compare-and-contrast-mocking-stubbing-and-faking/16699/6 "2018-07-15T21:29:47Z")

</div>

Generically, though this seems to be fairly simple from the outset, it was enlightening to slightly deep dive to understand the actual difference between Fake, Stubs and Mocks.

**Fake** objects actually have working implementations, but usually take some shortcut which makes them not suitable for production

**Stubs** provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it ‘sent’, or maybe only how many messages it ‘sent’.

**Mocks** are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

I try to simplify by using : Mock and Stub. I use Mock when it’s an object that returns a value that is set to the tested class. I use Stub to mimic an Interface or Abstract class to be tested. In fact, it doesn’t really matter what you call it, they are all classes that aren’t used in production, and are used as utility classes for testing.

More source here: [https://qakumar.wordpress.com/2018/07/15/day-11-compare-and-contrast-mockingstubbing-and-faking/](https://qakumar.wordpress.com/2018/07/15/day-11-compare-and-contrast-mockingstubbing-and-faking/)

---

<div class="post-metadata">

**Author:** ![youngdq232](https://avatars.discourse-cdn.com/v4/letter/y/49beb7/32.png) [@youngdq232](https://club.ministryoftesting.com/u/youngdq232)\
**Post date:** [19 July 2018 06:46 UTC](https://club.ministryoftesting.com/t/30-days-of-automation-in-testing-day-11-compare-and-contrast-mocking-stubbing-and-faking/16699/7 "2018-07-19T06:46:46Z")

</div>

Fakes  
A fake is a class that you can use instead of actual line of business code. This code contains no real business functionality; it is hard coded to return 5. However, it does provide two important benefits:  
It allows you to write your first unit test.  
It eliminates the need for a real Login Service, isolating dependent code from integration issues.

Stubs  
Martin Fowler defines Stubs as objects “that provide canned answers to calls made during the test.” This might seem the same as the fake written above, but the biggest difference is that a mocking framework like JustMock can be used to create the stub in the test, providing the necessary scaffolding for the system under test in very little code.

Mocks  
Mocks bring all of the benefits of stubs plus the ability to specify behavior. To demonstrate this, I will add behavior checking to the test. The first change is to verify that the arranged method was actually called. Using JustMock, this will require some minor changes to the test.

---

<div class="post-metadata">

**Author:** ![pabs616](https://sea2.discourse-cdn.com/flex020/user_avatar/club.ministryoftesting.com/pabs616/32/13139_2.png) [@pabs616](https://club.ministryoftesting.com/u/pabs616)\
**Post date:** [19 July 2018 18:59 UTC](https://club.ministryoftesting.com/t/30-days-of-automation-in-testing-day-11-compare-and-contrast-mocking-stubbing-and-faking/16699/8 "2018-07-19T18:59:42Z")

</div>

As I understand it (_and employed it_):

- Mocking - is creating data set or virtual UI that behaves in close proximity to the real thing
- Stubbing - is setting up a “template” or script with placeholder tags / hardcoded data
- Faking - is generating a simulated output action or response based on emulated input action

---

<div class="post-metadata">

**Author:** ![tienphan](https://avatars.discourse-cdn.com/v4/letter/t/ed655f/32.png) [@tienphan](https://club.ministryoftesting.com/u/tienphan)\
**Post date:** [8 August 2018 06:30 UTC](https://club.ministryoftesting.com/t/30-days-of-automation-in-testing-day-11-compare-and-contrast-mocking-stubbing-and-faking/16699/9 "2018-08-08T06:30:29Z")

</div>

#30daytestingchallenge  
#day11of 30daytestingchallenge

An interesting thread, about the difference Fake, Mock, Stub. You can refer to: [https://martinfowler.com/articles/mocksArentStubs.html](https://martinfowler.com/articles/mocksArentStubs.html)
