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
when(myClass, 'someMethod').thenReturn(someValue)
(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)
Great work Paul, i love the syntax of Mockito, looking forward to see how you develop this!
Where can this framework be found ? 🙂
I will be sharing the code via GitHub later this week 🙂
Apologies it has taken a little longer to make the framework available, I’m going to share a tutorial series first (starting next week) about how to use the framework.
I’m eager to see your solution, as I have been working on something similar and just recently (last night) found your blog. The timing is incredible!
I’ve continued working on a similar mocking framework that is use-syntax based on the Moq library for .NET: https://github.com/hollbl/ApexMoq
You appear to have a lot of experience using DI and mocking in Apex, so I’d love to get your thoughts on ways to improve the tool.
Sure I will try and take a look at your code at some point 🙂
Pingback: One Batch Apex to rule them all (Part II) – versiononefirst__c