• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

stub and mock

 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have googled the internet but still unable to grasp the difference between the two. So far, my understanding is with stub, you need to write the implementation of faked object by yourself, while wit mocks, you can delegate it to third party library. but then again, there are some tutorial that writes their own "mocks". this would mean my understanding is somehow flawed. and the second one is that stubbing is coarse-grained and mock is fine-grained. This is the most confusing of them all. Can anyone explain in more detail? Some code might make the issue clearer. thanks
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David,
I like Martin Fowler's paper on test doubles. His definitions are:

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.

Mocks are pre-programmed with expectations which form a specification of the calls they are expected to receive. They can throw an exception if they receive a call they don't expect and are checked during verification to ensure they got all the calls they were expecting.



I think this is the key difference - mocks are smarter than stubs. Most people use a framework to create the mocks in memory (with proxies and the like) using a framework like Easy Mock or jMock. Years ago, you had to write your mocks by hand. And for some types of mocks you still do tend to write them by hand. Like a mock subclass.
 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from martin fowler's mocksarentstubs paper, what I understand is mocks use behavior verification, while stubs use state verification. and therefore if you use mock, you're definitely using BDD instead of TDD. Is this about right? I also get the impression that stubs are becoming obsolette since more and more people are shifting to mocks (the "there's nothing that mocks can't do but stub can" argument). thanks
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just re-read MocksArentStubs.

David Spades wrote: and therefore if you use mock, you're definitely using BDD instead of TDD. Is this about right?


No. In fact the article even gives an example of TDD for mocks.

A mockist TDD practitioner, however, will always use a mock for any object with interesting behavior. In this case for both the warehouse and the mail service.



David Spades wrote: I also get the impression that stubs are becoming obsolette since more and more people are shifting to mocks (the "there's nothing that mocks can't do but stub can" argument). thanks


Libraries like jMock let you write stubs so people often think of them as mocks regardless of what they really are.
 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, now I'm getting confused again. so what's the difference between stub and mocks again? and how's their position in TDD and BDD? or are they orthogonal to each other?
thanks
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stubs provided canned answers. If I have 10 programs using the same stub, they all get the same values returned to them. Mocks allow you to verify parameters/calls and specify return values.

I think both stubs and mocks could be used with BDD or TDD. But maybe that means I'm not finicky enough about the terms. It'll be interesting to see if anyone else replies with a different opinion.
 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lets say a stand-in object is something like:


is IAmConfusing a stub? it has dumb getNumber, but then again, it has "not so dumb" getSeed.
thanks
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I consider that a stub. It certainly isn't a mock since you aren't setting expected values!
 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are doubles still allowed in integration test?
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's rare, but doubles could be used in an integration test. More likely to be stubs then.

For example, maybe I want to integration test the database and queues. However, the security service that tells me what role I am is not available locally. I use a double to set up that role and integration test everything else.
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic