Mocking test data
As discussed, it can be very expensive and slow to create all the test data we need for our tests to run. In many situations, our code will have dependencies that will make it difficult to test through either dynamic data (dates, exchange rates, and so on) or a complex setup process (many related Salesforce records—for example, when working with some data in solutions such as Salesforce CPQ or Salesforce Field Service). To facilitate testing our code more effectively, Salesforce provides a stub application programming interface (API) that allows us to build out a mocking framework for our code base. We can use the stub API by implementing the System.StubProvider
interface, which allows us to define a mocking framework that will return stubbed implementations of our classes and methods that are generated at runtime.
Mocking versus stubbing
Technically, mocking and stubbing are two different ways of allowing us to decouple our code and write tests. The...