apex-enterprise-patterns/fflib-apex-mocks
소개
Salesforce에서 제대로 된 단위 테스트를 수행하기 위한 Apex 모킹 프레임워크로, Stub API를 지원합니다.
개발 도구측정된 스타 증가
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- +2
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
OriginalFFLib ApexMocks Framework
ApexMocks is a mocking framework for the Salesforce Lightning Apex language.
It derives its inspiration from the well known Java mocking framework Mockito
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 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
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:
EXPECTED COUNT: 1
ACTUAL COUNT: 0
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();
Utilties
Setting a read-only field, such as a formula
Account acc = new Account();
Integer mockFormulaResult = 10;
acc = (Account)fflib_ApexMocksUtils.setReadOnlyFields(
acc,
Account.class,
new Map<SObjectField, Object> {Account.Your_Formula_Field__c => mockFormulaResult}
);
System.assertEquals(mockFormulaResult, acc.Your_Formula_Field__c);
Stub API
Using Salesforce's Stub API, stub objects are generated dynamically at run time.
fflib_ApexMocks mocks = new fflib_ApexMocks();
fflib_MyList mockList = (fflib_MyList)mocks.mock(fflib_MyList.class);
Documentation
- 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
관련 프로젝트 살펴보기
불러오는 중…