MOCK white box testing

Last night I was introduced to white box testing using nMock and POCMock tools. Mock testing, in a nutshell, provides the required framework to simplify white box testing. Scenarios can be defined that exercise specific code path in your application. Mock'ed classes and methods isolate part of the application code to return predetermined values or throw exceptions. This provide the benefit of:
- exercising hard to reproduce situations (such as specific date or network conditions)
- throw exceptions
- isolate part of your system

Mocking complements unit testing by allowing developers to "mock" part of the application while unit tests are being run: unit tests continue to provide pass/fail indication. I I used to do mock testing by replacing certain parts of the system/application I was building and replace it with a "stub", a fake functional equivalent. MOCK takes this idea and formalizes it, giving developers a structured way to mock part of their system, in the same way unit testing frameworks do (such as nUnit or soon, visual studio team system ;-).

However, it seems that MOCKing is much more immature than unit testing is. From what I gather, there are only 2 frameworks out there: nMOCK (open source) and POCMOCK (commercial app, see http://www.prettyobjects.com/POCMock.aspx?sid=Overview). Furthermore,
MOCKing is much more complex to setup and maintain than unit testing because the code is test case dependant. Each mock must validate parameters, then return values specific to those parameters. MOCKs can even throw exceptions!

Then again, I thought that MOCKs are especially useful in the following scenarios:
- web services: I was burned once designing a system that relied heavily on web services for its business logic. When these WS changed, unit tests could catch the changes but they prevented the rest of the application from passing. Having MOCKs in there would have allowed me to continue testing and even develop by completely de-coupling the web service provider
- database: all my web systems have a database running it. I always endup having some fancy database values for my tests and development. Sometimes I wish I could just have a MOCK that returns predefined values so that I can develop/test in peace.

Posted in | 9 comments