ApexMocks and Apex Enterprise Patterns
As we saw earlier, the supporting library, or Apex Enterprise Patterns, provides methods that provide a dependency injection facility through the factories in the fflib_Application
class. This facility is also compatible with the use of ApexMocks and the Apex Stub API. The following sections contain examples of the use of ApexMocks to unit test the layers within the application architecture introduced in earlier chapters.
Unit testing a controller method
The following test can be found in the RaceControllerTest
class and demonstrates how to mock a Service layer class:
@IsTest
private static void whenAwardPointsCalledIdPassedToService() {
fflib_ApexMocks mocks = new fflib_ApexMocks();
// Given
RaceServiceImpl mockService =
(RaceServiceImpl) mocks.mock(RaceServiceImpl.class);
Application.Service.setMock(RaceService.class, mockService);
// When
Id raceId = fflib_IDGenerator.generate(Race__c...