Testing software: fake vs stub

Vadim Samokhin picture Vadim Samokhin · Jul 24, 2011 · Viewed 9.7k times · Source

There are quite a few written about stub vs mocks, but I can't see the real difference between fake and stub. Can anyone put some light on it?

Answer

Thorarin picture Thorarin · Jul 24, 2011

I assume you are referring to the terminology as introduced by Meszaros. Martin Fowler does also mentions them regularly. I think he explains the difference pretty well in that article.

Nevertheless, I'll try again in my own words :)

A Fake is closer to a real-world implementation than a stub. Stubs contain basically hard-coded responses to an expected request; they are commonly used in unit tests, but they are incapable of handling input other than what was pre-programmed.

Fakes have a more real implementation, like some kind of state that may be kept for example. They can be useful for system tests as well as for unit testing purposes, but they aren't intended for production use because of some limitation or quality requirement.