Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

apex-enterprise-patterns/fflib-apex-mocks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 frameworkMockito

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 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();}}

verify() behaviour verification

// 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"]]

when() dependency stubbing

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

Utilties

Setting a read-only field, such as a formula

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

Stub API

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

Documentation

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors23

Languages


[8]ページ先頭

©2009-2025 Movatter.jp