Writing unit tests with the Apex Stub API
The Apex Stub API applies only within an Apex test context, so it cannot be used to implement DI outside of tests. To do that, you still need to leverage Apex interfaces. Utilizing Apex Stub APIs requires an understanding of the following:
- Implementing the StubProvider interface: The
System.StubProvider
system-provided Apex interface is effectively a callback-style interface. It allows your mocking code to be informed when method calls are against classes you are mocking in your test. You can implement this interface multiple times, once per class you’re mocking, or use a single implementation for building sophisticated generalized mocking frameworks. The ApexMocks open-source framework from the Apex Enterprise Patterns Open-Source Group (previously FinancialForce.com) is one such framework that we will be reviewing later. - Dynamic creation of stubs for mocking: The platform automatically creates instances of classes...