Skip to content
An Apex mocking framework for true unit testing in Salesforce, with Stub API support
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Added Ant build script and config for Travis CI Dec 9, 2016
src tabs Dec 27, 2017
.gitignore Add more setup steps to README Aug 16, 2017
.travis.yml Update .travis.yml Dec 9, 2016
LICENSE Create LICENSE Oct 6, 2014 Add more setup steps to README Aug 16, 2017
apex-mocks-generator-2.0.5.jar Release ApexMocks May 15, 2014
apex-mocks-generator-3.0.1.jar Release apex-mocks-generator 3.0.1 Jun 27, 2014
apex-mocks-generator-3.1.0.jar Various enhancements to ApexMocks: Nov 5, 2014
apex-mocks-generator-4.0.1.jar New mocks generator uses type forName for arg types Feb 20, 2016 Add more setup steps to README Aug 16, 2017
build.xml Add more setup steps to README Aug 16, 2017

FinancialForce ApexMocks Framework

Build Status

ApexMocks is a mocking framework for the Apex language.

It derives it's inspiration from the well known Java mocking framework Mockito

Deploy to Salesforce


  • Clone this repo
  • Copy the file into file and add your salesforce username and password.
  • Do ant deploy to get the classes in your dev org.

Using ApexMocks on

ApexMocks allows you to write tests to both verify behaviour 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 fflib_MyList.IList that we want to either verify or stub:

public class fflib_MyList 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

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

when() dependency stubbing

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


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.

	java -jar apex-mocks-generator-4.0.0.jar
		<Filepath to source files>
		<Filepath to interface properties file>
		<Name of generated mocks class>
		<Filepath to target files - can be the same as filepath to source files>
		<API version of generated mocks class - optional argument, 30.0 by default>

	//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);


You can’t perform that action at this time.