Implementing Mocking for Apex Tests

Trying to mock a actual database query can be hard. Using the fflib_Mocks jar you can generator Selector Mocks: More info here:

https://github.com/financialforcedev/fflib-apex-mocks

1. Create interface class that extends fflib_ISObjectSelector to mock the Selector 

2. Create interfacemocks.properties file in root of your project

3. Run java -jar apex-mocks-generator-4.0.0.jar “{path}/src/classes” “{path}/interfacemocks.properties” “fflib_Mocks” “{path}/src/classes” “30.0” to generate mocking files

4. Used mocked selector to write stub selector

5. Run mock generator as maven job

1. Create interface class that extends fflib_ISObjectSelector to mock the Selector 
public interface System_Selector extends fflib_ISObjectSelector {
    List<Object> selectByUserId(Id userId);
}

2. Create interfacemocks.properties file in root of your project

System_Selector=Contact:fflib_SObjectMocks.SObjectSelector
3. Run java -jar apex-mocks-generator-4.0.0.jar "{path}/src/classes" "{path}/interfacemocks.properties" "fflib_Mocks" "{path}/src/classes" "30.0" to generate mocking files
/* Generated by apex-mocks-generator version 4.0.1 */
@isTest
public class Selector_Mocks{
    public class Contact extends fflib_SObjectMocks.SObjectSelector implements System_Selector{
        private fflib_ApexMocks mocks;

        public Contact(fflib_ApexMocks mocks){
            super(mocks);
            this.mocks = mocks;
        }
        
        public List<Object> selectByUserId(Id userId){
            return (List<Object>) mocks.mockNonVoidMethod(this, 'selectByUserId', new List<Type> {System.Type.forName('Id')}, new List<Object> {userId});
        }
    }
}

4. Used mocked selector to write stub selector

  @isTest static void testContactSelector(){
        // Create mocks
  		fflib_ApexMocks mocks = new fflib_ApexMocks();
  		fflib_ISObjectUnitOfWork uowMock = new fflib_SObjectMocks.SObjectUnitOfWork(mocks);

  		System_Selector selectorMock = new System_Mocks.Contact(mocks);

  		// Given
  		mocks.startStubbing();
        List<Contact> testContactList = new List<Contact> {
  			new Contact(
  				Id = fflib_IDGenerator.generate(Contact.SObjectType),
  				FirstName = 'Test FirstName',
  				LastName = 'Test LastName',
  				Email = 'test@mail.com') };

        Set<Id> testContactSet = new Map<Id, Contact>(testContactList).keySet();
        User testUserId = new User(	Id = fflib_IDGenerator.generate(User.SObjectType),
  				FirstName = 'Test FirstName',
  				LastName = 'Test LastName',
  				Email = 'test@mail.com' );
        mocks.when(selectorMock.sObjectType()).thenReturn(Contact.SObjectType);
    		mocks.when(selectorMock.selectByUserId(testUserId.Id)).thenReturn(testContactList);
  		mocks.stopStubbing();

      System_Rest_App.Selector.setMock(selectorMock);

      // When
      new System_Mocks.Contact(mocks).selectByUserId(testUserId.Id);

      // Then
      ((System_Selector)
        mocks.verify(selectorMock)).selectByUserId(testUserId.Id);
    }

5. Run mock generator as maven job – ant generate.mocks

  <target name="generate.mocks">
		<java classname="com.financialforce.apexmocks.ApexMockGenerator">
			<classpath>
				<pathelement location="${basedir}/lib/apex-mocks-generator-4.0.1.jar"/>
			</classpath>
			<arg value="${basedir}/src/classes"/>
			<arg value="${basedir}/interfacemocks.properties"/>
			<arg value="Selector_Mocks"/>
			<arg value="${basedir}/src/classes"/>
		</java>
	</target>
Advertisements

One Comment on “Implementing Mocking for Apex Tests

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: