fflib-apex-mocks icon indicating copy to clipboard operation
fflib-apex-mocks copied to clipboard

There is no way to mock void methods

Open AllanOricil opened this issue 3 years ago • 2 comments

I have service classes which are used on OnBefore events that don't return anything. They literally just pass info to a Domain,which also does not return anything, to update my record data. I wanted to mock this service method and I couldn't because it is always expecting a "thenReturn()". I would like to mock this method because I want to use verify to check it has been called.

AllanOricil avatar Oct 22 '21 16:10 AllanOricil

@AllanOricil Skip specifying the mocks.when(...) statement. Simply do the verify. As in ...

fflib_ApexMocks mocks = new fflib_ApexMocks();
IMyService mockMyService = (IMyService) mocks.mock(IMyService.class);

// Execute the test implementing the void method

((IMyService) mocks.verify(mockMyService, 1))
	.doSomethingUseful(
		fflib_Match.sObjectWith(
			new Map<SObjectField, Object> {
				Contact.Id =>expectedId,
				Contact.Nickname__c => expectedNickname,
				Contact.LastSynchronized__c => expectedSyncedOn
}));

The "Matching" logic is optional but illustrates if needed.

stohn777 avatar Oct 22 '21 18:10 stohn777

@stohn777 cool. I will try that :D

AllanOricil avatar Oct 22 '21 21:10 AllanOricil