- Notifications
You must be signed in to change notification settings - Fork224
An Apex mocking framework for true unit testing in Salesforce, with Stub API support
License
apex-enterprise-patterns/fflib-apex-mocks
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
ApexMocks is a mocking framework for the Salesforce Lightning Apex language.
It derives its inspiration from the well known Java mocking frameworkMockito

ApexMocks allows you to write tests to both verify behavior and stub dependencies.
An assumption is made that you are using some form ofDependency Injection - for example passing dependencies via a constructor:
publicMyClass(ClassA.IClassAdependencyA,ClassB.IClassBdependencyB)
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:
publicclassfflib_MyListimplementsIList{publicinterfaceIList{voidadd(Stringvalue);Stringget(Integerindex);voidclear();BooleanisEmpty();}}
// Givenfflib_ApexMocksmocks =newfflib_ApexMocks();fflib_MyList.IListmockList = (fflib_MyList.IList)mocks.mock(fflib_MyList.class);// WhenmockList.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:
EXPECTED COUNT: 1ACTUAL COUNT: 0METHOD: EmailService__sfdc_ApexStub.sendTo(String)---ACTUAL ARGS: ("user-two@example.com")---EXPECTED ARGS: [[contains "user-one"]]
fflib_ApexMocksmocks =newfflib_ApexMocks();fflib_MyList.IListmockList = (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();
Accountacc =newAccount();IntegermockFormulaResult =10;acc = (Account)fflib_ApexMocksUtils.setReadOnlyFields(acc,Account.class,newMap<SObjectField,Object> {Account.Your_Formula_Field__c =>mockFormulaResult});System.assertEquals(mockFormulaResult,acc.Your_Formula_Field__c);
Using Salesforce'sStub API, stub objects are generated dynamically at run time.
fflib_ApexMocksmocks =newfflib_ApexMocks();fflib_MyListmockList = (fflib_MyList)mocks.mock(fflib_MyList.class);
- ApexMocks Framework Tutorial
- Simple Dependency Injection
- ApexMocks Generator
- Behaviour Verification
- Stubbing Dependencies
- Supported Features
- New Improved apex-mocks-generator
- ApexMocks Improvements - exception stubbing, base classes and more
- Matchers
- ApexMock blogs from Jesse Altman
- Order of calls verification
- Answering
- Counters
- Troubleshooting
About
An Apex mocking framework for true unit testing in Salesforce, with Stub API support
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.