Unit testing a Service method

The following test method can be found in the RaceServiceTest class and demonstrates how to mock classes implementing the Unit Of Work, Domain, and Selector layers:

@isTest 
private static void
whenAwardChampionshipPointsCallsDomainAndCommits() { fflib_ApexMocks mocks = new fflib_ApexMocks(); // Given - Create mocks fflib_SObjectUnitOfWork mockUow = (fflib_SObjectUnitOfWork)
mocks.factory(fflib_SObjectUnitOfWork.class); RacesSelector mockSelector = (RacesSelector) mocks.factory(RacesSelector.class); Contestants mockDomain = (Contestants) mocks.factory(Contestants.class); // Given - Configure mock responses Id testRaceId = fflib_IDGenerator.generate(Race__c.SObjectType); Id testContestantId =
fflib_IDGenerator.generate(Contestant__c.SObjectType); List<Race__c> testRacesAndContestants = (List<Race__c>) fflib_ApexMocksUtils.makeRelationship( List<Race__c>.class, new List<Race__c> { new Race__c ( Id = testRaceId) }, Contestant__c.Race__c, new List<List<Contestant__c>> { new List<Contestant__c> { new Contestant__c (Id = testContestantId) } }); mocks.startStubbing(); mocks.when(mockSelector.SObjectType()). thenReturn(Race__c.SObjectType); mocks.when(mockSelector.selectByIdWithContestants(
new Set<Id> { testRaceId })).
thenReturn(testRacesAndContestants);
mocks.when(mockDomain.SObjectType()).
thenReturn(Contestant__c.SObjectType); mocks.stopStubbing(); // Given - Inject mocks Application.UnitOfWork.setMock(mockUow); Application.Selector.setMock(mockSelector); Application.Domain.setMock(mockDomain); // When RaceService.awardChampionshipPoints(
new Set<Id> { testRaceId }); // Then ((RacesSelector) mocks.verify(mockSelector, 1)).
selectByIdWithContestants(new Set<Id> { testRaceId }); ((Contestants) mocks.verify(mockDomain, 1)).
awardChampionshipPoints(mockUow); ((fflib_SObjectUnitOfWork) mocks.verify(mockUow, 1)).
commitWork(); }

Consider the following notable aspects of the preceding test code:

  • The fflib_ApexMocksUtils.makeRelationship method is used to construct in memory a list of records containing child records. This utility method (part of ApexMocks) works around a platform limitation in mocking this data construct. This method is very useful for returning more complex record sets returned from the Selector methods your tests are mocking.
  • Prior to calling the Application.Selector.setMock and Application.Domain.setMock methods to inject the mock implementations of the RacesSelector and Contestants Domain classes, the mocking framework is used to mock responses to the SObjectType methods for the mocked instances of these classes, which is called by the setMock methods. Mocking this method ensures that the mocked Selector instance gets correctly registered with the Selector factory.
  • The fflib_IDGenerator.generate method (part of ApexMocks) is used to generate an in-memory ID that is used to populate the mock records returned from the mock Selector. These same IDs are used to confirm that what was passed into the Selector is the same IDs that were passed to the Service method.
  • The Contestants Domain class gained a default constructor, as required by the Test.createStub method, used by the fflib_ApexMocks.factory method.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.176.88