0

I am using fflib mocks and at4dx

Trying to test a simple controller method which is calling a selector method.

Controller Method:

public with sharing class MyController {
   public static String getFieldsByFieldSetName() {
        return JSON.serialize(new MyObjectsSelector().getFieldSets());
    }
}

Selector Class:

public with sharing class MyObjectsSelector extends ApplicationSObjectSelector implements IMyObjectsSelector, IApplicationSObjectSelector {
    public Schema.SObjectType getSObjectType() {
        return MyObject__c.sObjectType;
    }

    public static MyObjectsSelector newInstance(){
        return (IMyObjectsSelector) Application.Selector.newInstance(MyObject__c.SObjectType);
    }

    public override List<Schema.SObjectField> getSObjectFieldList() {
        List<Schema.SObjectField> defaultFields = new List<Schema.SObjectField> {
            MyObject__c.Id
        };

        return defaultFields;
    }

    public override String getOrderBy() {
        return 'Name DESC';
    }

    private List<Schema.SObjectField> getAdditionalSObjectFieldList() {
        return new List<Schema.SObjectField> {};
    }

    public List<FieldSetWrapper> getFieldSets() {
        List<FieldSetWrapper> fieldSetsWrappers = new List<FieldSetWrapper>();
        Map<String, Schema.FieldSet> fieldSetByName = getSobjectType().getDescribe().fieldSets.getMap();

        for (String fieldSetName: fieldSetByName.keySet()) {
            Schema.FieldSet fieldSet = fieldSetByName.get(fieldSetName);
            fieldSetsWrappers.add(new FieldSetWrapper(fieldSet));
        }

        return fieldSetsWrappers;
    }

    public class FieldSetWrapper {
        public FieldSetWrapper(Schema.FieldSet fieldSet) {
            this.fieldSetLabel = fieldSet.getLabel();
            this.fieldSetName = fieldSet.getName();
            this.fields = fieldSet.getFields();
        }

        public string fieldSetLabel;
        public String fieldSetName;
        List<Schema.FieldSetMember> fields;
    }
}

Selector Interface

public interface IMyObjectsSelector extends fflib_ISObjectSelector {
    List<MyObjectsSelector.FieldSetWrapper> getFieldSets();
}

Unit Test Class

@isTest
private class MyControllerClassTest {
    @isTest static void testGetFieldsByFieldSetName() {
        fflib_ApexMocks mocks = new fflib_ApexMocks();
        //configure mocks
        MyObjectsSelector mockSelector = (MyObjectsSelector) mocks.mock(MyObjectsSelector.class);
        MyObjectsSelector.FieldSetWrapper fieldSetWrapper = 
            new MyObjectsSelector.FieldSetWrapper(
                fflib_ApexMocksUtilsTest.findAnyFieldSet()
            );
        mocks.startStubbing();
        mocks.when(mockSelector.SObjectType()).thenReturn(MyObject__c.SObjectType); // REQUIRED
        mocks.when(mockSelector.getFieldSets()).thenReturn(
            new List<MyObjectsSelector.FieldSetWrapper>{fieldSetWrapper}
        );
        mocks.stopStubbing();

        // Given - Inject mocks
        Application.Selector.setMock(mockSelector);

        // When
        String output = MyController.getFieldsByFieldSetName();

        //Then
        ((MyObjectsSelector) mocks.verify(mockSelector, 1)).getFieldSets();
    }
}

I am getting error as:

fflib_ApexMocks.ApexMocksException: Expected : 1, Actual: 0 -- Wanted but not invoked: MyObjectsSelector__sfdc_ApexStub.getFieldSets().

Can anyone please let me know if I am doing anything wrong here?

Thanks in Advance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.