Need help with fflib-apex-mocks?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

259 Stars 155 Forks BSD 3-Clause "New" or "Revised" License 247 Commits 10 Opened issues


An Apex mocking framework for true unit testing in Salesforce, with Stub API support

Services available


Need anything else?

Contributors list

FFLib ApexMocks Framework

Push Source and Run Apex Tests

ApexMocks is a mocking framework for the Salesforce Lightning Apex language.

It derives its inspiration from the well known Java mocking framework Mockito

Deploy to Salesforce

Using ApexMocks on the Salesforce Lightning Platform

ApexMocks allows you to write tests to both verify behavior and stub dependencies.

An assumption is made that you are using some form of Dependency Injection - for example passing dependencies via a constructor:

public MyClass(ClassA.IClassA dependencyA, ClassB.IClassB dependencyB)
This allows you to pass mock implementations of dependencies A and B when you want to unit test MyClass.

Lets assume we've written our own list interface fflibMyList.IList that we want to either verify or stub: ```Java public class fflibMyList implements IList { public interface IList { void add(String value); String get(Integer index); void clear(); Boolean isEmpty(); } } ```

verify() behaviour verification

// Given
fflib_ApexMocks mocks = new fflib_ApexMocks();
fflib_MyList.IList mockList = (fflib_MyList.IList)mocks.mock(fflib_MyList.class);

// When mockList.add('bob');

// Then ((fflib_MyList.IList) mocks.verify(mockList)).add('bob'); ((fflib_MyList.IList) mocks.verify(mockList, fflib_ApexMocks.NEVER)).clear();

If the method wasn't called the expected number of times, or with the expected arguments, verify will throw an exception. The exception message contains details of the expected and actual invocations:

METHOD: EmailService__sfdc_ApexStub.sendTo(String)
ACTUAL ARGS: ("[email protected]")
EXPECTED ARGS: [[contains "user-one"]]

when() dependency stubbing

fflib_ApexMocks mocks = new fflib_ApexMocks();
fflib_MyList.IList mockList = (fflib_MyList.IList)mocks.mock(fflib_MyList.class);

mocks.startStubbing(); mocks.when(mockList.get(0)).thenReturn('bob'); mocks.when(mockList.get(1)).thenReturn('fred'); mocks.stopStubbing();


Setting a read-only field, such as a formula

Account acc = new Account();
Integer mockFormulaResult = 10;
acc = (Account)fflib_ApexMocksUtils.setReadOnlyFields(
        new Map {Account.Your_Formula_Field__c => mockFormulaResult}
System.assertEquals(mockFormulaResult, acc.Your_Formula_Field__c);

Stub API

ApexMocks now implements the Stub API!

Previously, stub objects had to be generated using the ApexMocks generator at compile time. Now, stub objects can be generated dynamically at run time.

fflib_ApexMocks mocks = new fflib_ApexMocks();
fflib_MyList mockList = (fflib_MyList)mocks.mock(fflib_MyList.class);
You can continue to use the ApexMocks generator, if you wish, but this is no longer a prerequisite to using ApexMocks.

Generating Mock files

Run the apex mocks generator from the command line. ```Bash java -jar apex-mocks-generator-4.0.0.jar

E.g. the command used to generate the current version of fflib_Mocks.

java -jar apex-mocks-generator-4.0.0.jar "/Users/jbloggs/Dev/fflib-apex-mocks/src/classes" "/Users/jbloggs/Dev/fflib-apex-mocks/" "fflib_Mocks" "/Users/jbloggs/Dev/fflib-apex-mocks/src/classes" "30.0" ```

Instantiate the generated classes as follows:

fflib_ApexMocks mocks = new fflib_ApexMocks();
fflib_MyList.IList mockList = new MockMyList(mocks);


We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.