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. For this you still need to leverage Apex interfaces.
Utilizing the APIs requires an understanding of the following:
- Implementing the Stub Provider 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 a single implementation for building sophisticated generalized mocking frameworks. The Apex Mocks open source framework from 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 you wish to mock through the
Test.createStub
method. You do not need to create Apex interfaces to...