Panasonic Youth

Naming test objects - mock, stub, fake?

Martin Fowler recently posted about the confusion regarding what to name test doubles, and proposes a standard from Gerard Meszaros’ upcoming book. The names are as follows, with descriptions paraphrased from the using test double page at the books outline: XUnit Test Patterns : Refactoring Test Code

  • Dummy Object: placeholder object that is passed to the code under test as a parameter but never used
  • Test Stub is an object that is used by a test to replace a real component to force the system down the path we want for the test. A stub may also record info about how it was called (ie stub.wasMethodCalled() or stub.numberOfTimesMapResultSetCalled()
  • Mocks fake implementation by returning hard coded values or values preloaded - they can also verify that the correct calls were made in the right order
  • Fake objects replace the functionality of the of the real object with an alternate implementaiton, ie returning a canned list of values instead of hitting a database

I’m not sure if these names jive with what I am used to, but as long as the community can agree on some sort of consistent standard and stick with it I will happily adopt it. Trying to explain the difference between an EasyMock provided mock and a canned “fake object” gets tiring.