fflib-apex-common-samplecode icon indicating copy to clipboard operation
fflib-apex-common-samplecode copied to clipboard

Ambiguous method signature: void setMock

Open EmilioMN92 opened this issue 4 years ago • 1 comments
trafficstars

When creating an AccountsServiceTest (similar to the OpportunitiesServiceTest in the sample), I have the following error:

Ambiguous method signature: void setMock.

The test method:

@IsTest
	private static void testSync(){
        // Create mocks
		fflib_ApexMocks mocks = new fflib_ApexMocks();
		
		fflib_ISObjectUnitOfWork uowMock = (fflib_ISObjectUnitOfWork) mocks.mock(fflib_ISObjectUnitOfWork.class);
		IAccounts domainMock = (IAccounts) mocks.mock(IAccounts.class);
		IAccountsSelector selectorMock = (IAccountsSelector) mocks.mock(IAccountsSelector.class);


		// Given
		mocks.startStubbing();
		List<Account> testAccsList = new List<Account> { 
			new Account(
				Id = fflib_IDGenerator.generate(Account.SObjectType),
				Name = 'Clinic Test',
                RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get(Label.RecordType_BusinessAccount).getRecordTypeId(),
                Type = 'IVF Clinic', 
                Subsidiary__c = 'SPAIN', 
                Category__c = 'A', 
                BillingCountry = 'Spain', 
                SRMExternalId__c = '2222')};
		Set<Id> testAccSet = new Map<Id, Account>(testAccsList).keySet();
		mocks.when(domainMock.sObjectType()).thenReturn(Account.SObjectType);
		mocks.when(selectorMock.sObjectType()).thenReturn(Account.SObjectType);
		mocks.when(selectorMock.selectById(testAccSet)).thenReturn(testAccsList);
		mocks.stopStubbing();
		Application.UnitOfWork.setMock(uowMock);
		Application.Domain.setMock(domainMock);
		Application.Selector.setMock(selectorMock);

		// When
		AccountsService.sync(testAccSet);

		// Then
		((IAccounts) 
			mocks.verify(domainMock)).sync(uowMock);
		((fflib_ISObjectUnitOfWork) 
			mocks.verify(uowMock, 1)).commitWork();
    }

My IAccounts is like this: public interface IAccounts extends fflib_ISObjectDomain { void sync(fflib_ISObjectUnitOfWork uow); }

In order to patch it, I added a new setMock function that receives an IAccounts as the parameter, but it is obviously a patch. Can anyone please tell me why am I getting this error?

Thanks in advance.

EmilioMN92 avatar Apr 29 '21 13:04 EmilioMN92

Hi @EmilioMN92

I think I see what might be happening here, but I cannot reproduce with a more simple test, included below. I'd have to do a bit more research.

May I suggest reducing your case to its bare essentials, which may uncover some overlooked bit of information.


IBase.cls

public interface IBase {
    void One();
}

InterfaceResolveTest.cls

@Istest
private class InterfaceResolveTest {

    @IsTest
    private static void test() {
        ISuper suuper = (ISuper) Test.createStub(ISuper.class, new Stubby());
        System.assertEquals('ISuper', new InterfaceResolveTest().setMock(suuper));
    }
    
    private String setMock(IBase mock) {
        return 'IBase';
    }
    
    private String setMock(ISuper mock) {
        return 'ISuper';
    }
}

ISuper.cls

public interface ISuper extends IBase {
    void Two();
}

Stubby.cls

public class Stubby implements StubProvider {

    public Object handleMethodCall(
        Object stubbedObject, 
        String stubbedMethodName, 
        Type returnType, 
        List<Type> listOfParamTypes, 
        List<String> listOfParamNames, 
        List<Object> listOfArgs) {
        
        return this;
    }
}

john-storey-devops avatar May 18 '21 22:05 john-storey-devops