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 Application
class. This facility is also compatible with the use of ApexMocks and 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.factory(RaceServiceImpl.class); Application.Service.setMock(RaceService.class, mockService); // When Id raceId = fflib_IDGenerator.generate(Race__c.SObjectType); RaceController raceController =...