What is the difference between a Stub, a Fake, and a Dummy? Well, as a refresher from Gerard Meszaros’s Test Double Patterns and Martin Fowler’s Mocks Aren’t Stubs:
- Mock – Sets expectations on how it is used by the sut
- Stub – Provides canned responses
- Fake – Has a real, but light-weight ‘shortcut’ implementation
- Spy – Records information about calls
- Dummy – ‘Filler’ object that is passed but never actually used
Let’s look at an example:
class PersonTest { def void testSetAddress) { def sut = new Person() sut.setAddress(addressDouble) assert sut.getAddress == addressDouble } def getAddressDouble() { return new Address(){ @Override def getLine1() { return '' } @Override def getLine2() { return '' } } } }
Is the AddressDouble
a Stub, Fake, or Dummy? Well obviously, it is a Stub, as it only provides canned responses. But can this also be a Dummy? I think so. According to the above definition of a Dummy, it just depends on where the object is used. If it only fills a parameter list, but never used, it is a Dummy. If it is used, then it can be a Stub.
But it is not a Fake, since it provides no real implementation. It will always answer empty strings, even if Address
has mutation methods and you used them.
We generally abuse these terms. We have many test doubles with names that look like PersonStub
, but these are really Fakes, providing some shortcut implementations. We have DummyPerson
, but these are really Stubs, used all over the place, offering up canned responses. Most of the time, null
is the only real Dummy we use.
Originally Posted on my Blogger site May of 2014