The goals I set myself in creating a mocking framework for the Force.com platform are:
- minimal impact on production code
- ease of use
- mockito style syntax
Mockito is a well known mocking framework for Java development. Personally I love the syntax it provides, it allows for the creation of very readable unit tests.
Any impact on production code should be absolutely minimal, ideally zero. We don’t want to litter production code with instrumentation code, nor do we want to have to write our production code in a way we wouldn’t have done just to use a mocking framework.
Keep in mind mocking makes the assumption we will be using dependency injection, so we also need a simple, light-weight approach for DI too.
So given all of that, the kinds of basic requirements I would envisage for a mocking framework are:
(1) basic when style stubbing
(2) specific argument matching
when(myClass, 'someMethodTakingStringArgument', 'argumentValue').thenReturn(someValue) when(myClass, 'someMethodTakingIntegerArgument', 1).thenReturn(someValue)
(3) verify style behaviour verification
verify(myClass, 'someMethod’) verify(myClass, 'someMethod', argumentValues)
(4) exception stubbing
when(myClass, 'someMethod').thenThrow(new MyException())
(5) argument matchers
when(myClass, 'someMethod', anyInteger()).thenReturn(someValue)