2

Given the following method, I would like to verify the registerNew/registerRelationship using Apex Mocks on my unit tests, basically to know if the record is going to be created as expected.

public Opportunity createOpportunity(fflib_ISObjectUnitOfWork uow, Contact c, Id userId){

Opportunity opp = new Opportunity(
        name                        = 'First Opp',
        StageName                   = 'First step',
        CloseDate                   =  Date.today(),
        OwnerId                     =  userId
);

uow.registerNew(opp,Opportunity.ContactId, c);
uow.registerRelationship(opp,Opportunity.Contacto__c, c);

return opp;

}

I am not able to find any documentation where I can understand how to do it, only found some articles registering sObjects without any relation like uow.registerNew(contacts) and therefore verifying later, for example, using the following piece of code:

((fflib_ISObjectUnitOfWork) mocks.verify(unitOfWorkMock)).registerNew(fflib_Match.sObjectWith(
            new Map<SObjectField, Object>{
                    Contact.FirstName => requestDTO.consumer.consumerName}
));
2
  • The last piece of code is just checking if method was called with given set of parameters.But you are asking about how to mock it. Given methods (registerNew nor registerRelationship do not return anything, they just update unitOfWork). Any reason why you want to mock those? Knowing that might help to answer your question
    – kurunve
    Oct 20, 2021 at 16:37
  • @kurunve My idea is to test if really the opportunity was created as expected.. Oct 20, 2021 at 17:12

1 Answer 1

2

So, to verify your calls to uow your test class needs to do these things:

// Given an instance of ApexMocks
fflib_ApexMocks mocks = new fflib_ApexMocks();

// Given an instance of a mock UnitOfWork class
fflib_SObjectUnitOfWork mockUow = (fflib_SObjectUnitOfWork) mocks.mock(fflib_SObjectUnitOfWork.class);

// Given mock instance injected into the fflib factories
Application.UnitOfWork.setMock(mockUow);

// Given mock data
Contact mockContact = new Contact(Id = fflib_IDGenerator.generate(Contact.SObjectType));

// when execute your code under test
new MyClass().createOpportunity(mockUow,mockContact,UserInfo.getUserId()); 
             

// then verify unitOfWork method calls
((fflib_ISObjectUnitOfWork) mocks.verify(unitOfWorkMock))
   .registerNew(
     fflib_Match.sObjectWith(
        new Map<SObjectField, Object>{
                Opportunity.Name => 'First Opp',
                Opportunity.StageName => 'First step',
                Opportunity.CloseDate => Date.today(),
                Opportunity.OwnerId => UserInfo.getUserId()}),
      fflib_Match.eqSObjectField(Opportunity.ContactId),
      fflib_Match.sObjectWith(
        new Map<SObjectField, Object>{
                Contact.Id => mockContact.Id})
));

Note that the verify matches all three arguments of your

uow.registerNew(opp,Opportunity.ContactId, c);

Here is the second verify for uow.registerRelationship(opp,Opportunity.Contacto__c, c);

((fflib_ISObjectUnitOfWork) mocks.verify(unitOfWorkMock))
   .registerRelationship(
     fflib_Match.sObjectWith(
        new Map<SObjectField, Object>{
                Opportunity.Name => 'First Opp',
                Opportunity.StageName => 'First step',
                Opportunity.CloseDate => Date.today(),
                Opportunity.OwnerId => UserInfo.getUserId()}),
      fflib_Match.eqSObjectField(Opportunity.Contacto__c),
      fflib_Match.sObjectWith(
        new Map<SObjectField, Object>{
                Contact.Id => mockContact.Id})
));
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .