Test Doubles: A Taxonomy of Pretend Objects

January 8, 2007

Ned recently pointed out Martin Fowler's article Mocks Aren't Stubs.

The vocabulary for talking about [pretend objects] soon gets messy - all sorts of words are used: stub, mock, fake, dummy. For this article I'm going to follow the vocabulary of Gerard Meszaros's upcoming book. It's not what everyone uses, but I think it's a good vocabulary and since it's my essay I get to pick which words to use. Meszaros uses the term Test Double as the generic term for any kind of pretend object used in place of a real object for testing purposes.

Personally, I like to think of them as Stunt Objects. Meszaros, via Fowler, defines the following taxonomy of pretend objects:

dummy.jpg

Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.

fake.jpg

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

stubs.jpg

Stub objects provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.

mock.jpg

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

Posted by Jeff Atwood
28 Comments

Admit it, you just wanted to post a picture of boobies.

Dan Hulton on January 8, 2007 10:24 AM

Marcus, I use mocks almost exclusively when writing my unit tests because if you attempt to have the thing you are unit testing use actual objects it quickly becomes a (complicated) systems test instead. Mocks can also let you do some tests that are otherwise hard-to-impossible to test, such as asserting that your object under unit test only makes some call once (this is useful where that call is possibly long-running or state-modifying in some way that is hard to test).

Max Bolingbroke on January 9, 2007 1:13 AM

If I'm not mistaken, I believe that picture comes from the Victoria's Secret catalog. October 2005 edition. Page 27.

DT on January 9, 2007 1:32 AM

And boobies dominate over ood stuff. I'm confident about fate of this world :).

cwiz on January 9, 2007 2:01 AM

Marcus

As i understand it, mock objects are usually used in unit testing situations: a unit test should have as few dependencies as possible, ideally it should only depend on the object/method that is being tested.

The issue is that objects are very often dependent on one another (composed with other objects, ...), and your unit tests therefore end up testing what you want to test, but also every object in the composition hierarchy of what you want to test.

Mock objects are used to remove this composition hierarchy by composing the tested object with mock objects, therefore removing all dependencies, and allowing you to test only what you want to test, and nothing else.

Masklinn on January 9, 2007 2:47 AM

They look to me like they're ready for production.

Mako on January 9, 2007 4:43 AM

Marcus, it is good to take a look at the original mock objects paper too: (pdf) http://www.connextra.com/aboutUs/mockobjects.pdf. As I understand them, mock objects are like little spies that can be inserted into other objects to check whether they are being used as intended. They also have the good property of breaking dependencies and can also work as stubs providing canned data.

Thiago Arrais on January 9, 2007 5:01 AM

Great picture choices. Insert here appropriate or inappropriate witty remark here about the silicone twins.

I really love the choice for 'mock'. 1) It's a perfect choice. I really can't think of anything that would have been as good. 2) I'm an avid Lewis Carroll and John Tenniel fan and love seeing the Alice artwork get recognition.

dinah on January 9, 2007 5:02 AM

Mock, dummy, fake .. what is this. Name your stub category day? They all STUBS. Why make it more confusing?

Look I just created a dummy object that morphs into a fake when stubed in my fake test...

cland on January 9, 2007 6:47 AM

I was hopping to pop over here and make some clever comment about the imagery used in this post. However, it looks like any sophmoric quip I could have come up with has already been covered. Excellent job, gents.

Dan Hounshell on January 9, 2007 7:46 AM

had a hard time explaining to my female co-worker at office that I was reading a technical blog. Thanks.

Puneet on January 9, 2007 7:47 AM

I'm with dinah. It filled my heart with joy to see the mock turtle.

[ICR] on January 9, 2007 8:41 AM

What I want to know is what is that piece of hay/grass sticking down in the boob shot on the right? It looks like the lady has something in her mouth and you cropped it all out. If you haven't done any cropping then it is a complete mystery as to why this would be there in the first place. Please elaborate!

matt on January 9, 2007 8:43 AM

Caprice Bourret. I'd recognize those anywhere.

JRK on January 9, 2007 9:45 AM

Matt, I'm pretty sure that's a strand of hair, not hay/grass.

Shadow Wolf on January 9, 2007 9:58 AM

It was difficult to come up with a better image choice for "Fake". "Mock", on the other hand, is incredibly difficult.

Jeff Atwood on January 9, 2007 12:12 PM

I never really understood what the whole "mocking" business is all about. Why not just implement the objects to begin with?

Marcus Stade on January 9, 2007 12:31 PM

How hard was it to find a picture of boobs that actually had something covering them?

Tyler on January 9, 2007 12:35 PM

Thanks for the pointers you guys!

Marcus Stade on January 10, 2007 3:27 AM

I wanted to show a co-worker the funny Ping video game from a previous post, but I nearly had a panic attack when the "Fake" picture popped onto his screen. I think your blog is excellent, and I appreciate the feminine form as much as any man, but PLEASE don't put things on your site that might get me fired.

Boo Boo Honeypaw on January 10, 2007 4:34 AM

Jesus. it's a pair of covered tits. It's not like you are browsing pr0n. If you get fired for that small amount of skin above you should be glad to be out and not working for such an uptight employer.

Josh on January 10, 2007 7:19 AM

Maybe you shouldn't be reading Coding Horror at work anyway. That's why they call it "work" and not "play time".

Matt on January 10, 2007 10:45 AM

Haven't you read the "study" showing that people who read tech blogs are more productive and better programmers (or whatever it is we do)?

Perfect justification for reading blogs at work, I'd say. It's "professional development."

Spencer on January 11, 2007 2:16 AM

Regarding cland's comment:

The category is "testing doubles" or "substitutes", not "stubs".
I agree with the taxonomy completely, but would propose one more:

Sometimes your test double derives from the actual object under test.
I propose calling these "self mocks", "self stubs", etc.

In general when you find yourself doing this it indicates other problems in your code. However, it is sometimes necessary for the purposes of making something visible, or for overriding methods to substitute only part of the object.

Dave Mann on April 3, 2007 11:37 AM

it's funny how terms take hold. it's unfortunate in some sense.

for instance, the term 'stub' barely has any semantic relevance. at best it would be as a receipt of transaction which we could take to mean recording the method calls, but this is becoming known as a test 'spy'. or is it that the stub stands in for the check? honestly i haven't slightest why 'stub' was every the selected term.

then there is the english meaning of 'mock' which really conveys nothing about verification, but only the idea of something that pretends to be something else. in that sense all test doubles are mocks, even if ever so scantily. left to my intuition i'd expect a mock to be what dave mann called a 'self mock'. and for mock, something like 'appraiser' --you know, someone with a checklist, seems more fitting.

is it valuable to use terms with general meanings more akin to a specialized use? i think so. it saves brain cells. unfortunately I think we love out DD-style realm building too much to actually do so.

trans on October 21, 2008 4:57 AM

Seriously. Covered or not, I'm yet another person who reads this blog at work and would have a hard time explaining that one off.

Elf/Vulcan ears or cat-eye contacts or an obvious toupee or an obvious nose-job -- anything else would be more SFW.

Just another $0.02. Thanks for the (in general) all around fantastic blog. :)

Clint Herron on February 6, 2010 9:52 PM

Thanks for the taxonomy. It certainly clarifies what (some) people mean when they talk about such things.

I do not feel a day-to-day need to make such fine distinctions between dummy, fake, stub and mock objects - I know what I need when I need it - but I applaud you for doing it for me.

Frankly, I rarely find the need to make such fine distinctions between the different types of "testing" objects identified but it will make reading Mr Fowler's articles a lot clearer.

Many thanks.

Gordon J Milne on February 6, 2010 9:52 PM

;[;][;ju87uy

sldfk123 on May 10, 2010 10:53 PM

The comments to this entry are closed.