Allen begins with a sharp argument to what's likely the most common misconception about mocks:
Some people have a misconception that mock objects are only useful if you need to simulate interaction with a resource that is difficult to use in unit tests - like an object that communicates with an SMTP server. This isn't true.He follows with a reference to Colin Mackay's article on mocks listing some common scenarios mocks are useful:
Allen then gets to the meat of his message when he implies that even the list above may be a tad shortsighted, asserting more generally that "test doubles [mocks] are useful when you want to isolate code under test." In short, according to Allen mocks are to be used to keep the tests for business components independent of all other components the component under test is dependent on; 'A' uses 'B', 'A unit test' should break only if 'A' is broken, regardless of the condition of 'B'.
- The real object has nondeterministic behavior
- The real object is difficult to setup
- The real object has behavior that is hard to trigger
- The real object is slow
- The real object is a user interface
- The real object uses a call back
- The real object does not yet exist
The article continues with a tie-in to the role of mock objects in true test-driven development:
The authors of "Mock Roles, Not Objects" say that mocks are:Allen concludes with a short discussion about the use of mock object frameworks, such as JMock, EasyMock, and NUnit. In summary, his assertion is that mock object frameworks themselves are rather simple although effective use of them can be tricky.
"… a technique for identifying types in a system based on the roles that objects play … In particular, we now understand that the most important benefit of Mock Objects is what we originally called interface discovery".
A related discussion worth checking out just surfaced on the TDD Yahoo group, check it out at here.