When it comes to building a robust test suite for an enterprise application, the mocking technique is a must have in our developer tool-belt. It is a big enabler for crafting well architected applications and it is a well known standard in the industry for most of the advanced programming languages. However it is not that widely used in the Salesforce community.
The thing is that the platform as such was not facilitating the task until the more-or-less recent delivery of the Stub API. Thus, the Salesforce developers were generally doing integration testing instead of true unit testing which is quite problematic, especially in the medium-long term and bigger applications. Integration tests are necessary, but its presence should be much lower.
Fortunately, a mocking framework based on Mockito, ApexMocks, came to the rescue back in 2014. Like Mockito, it is extremely useful and full of features. It is popular in the enterprise world, but surprisingly not that much adopted as one would expect. The truth is that the Salesforce engineering experience and history is very wide and different. But hopefully if you are reading this is because you are looking to know how to adopt ApexMocks into your project (and spread the word). So, let’s dive in!