All Projects → apex-enterprise-patterns → Fflib Apex Mocks

apex-enterprise-patterns / Fflib Apex Mocks

Licence: bsd-3-clause
An Apex mocking framework for true unit testing in Salesforce, with Stub API support

Projects that are alternatives of or similar to Fflib Apex Mocks

mockingbird
🐦 Decorator Powered TypeScript Library for Creating Mocks
Stars: ✭ 70 (-72.33%)
Mutual labels:  mock, unit-test
amoss
Amoss - Apex Mock Objects, Spies and Stubs - A Simple Mocking framework for Apex (Salesforce)
Stars: ✭ 55 (-78.26%)
Mutual labels:  mock, apex
automock
A library for testing classes with auto mocking capabilities using jest-mock-extended
Stars: ✭ 26 (-89.72%)
Mutual labels:  mock, unit-test
kmpapp
👨‍💻 Kotlin Mobile Multiplatform App (Android & iOS). One Code To Rule Them All. MVVM, DI (Kodein), coroutines, livedata, ktor, serialization, mockk, detekt, ktlint, jacoco
Stars: ✭ 34 (-86.56%)
Mutual labels:  mock, unit-test
xrm-mock-generator
📖  Generates a mock Xrm.Page object. Commonly used by xrm-mock to test Dynamics 365 client-side customisations.
Stars: ✭ 15 (-94.07%)
Mutual labels:  mock, unit-test
xrm-mock
📚 A fake implementation of the Xrm object model. Written in TypeScript against @types/xrm definitions.
Stars: ✭ 64 (-74.7%)
Mutual labels:  mock, unit-test
mockrequire
Simple module for mocking required dependencies. Works with any testing suite.
Stars: ✭ 17 (-93.28%)
Mutual labels:  mock
php-mock-prophecy
Mock built-in PHP functions (e.g. time()) with Prophecy (phpspec).
Stars: ✭ 16 (-93.68%)
Mutual labels:  mock
JTK
JTK is a library designed for writing applications and libraries in C. It provides core utilities such as collections, unit testing, I/O streams, threads and much more.
Stars: ✭ 25 (-90.12%)
Mutual labels:  unit-test
robot-testing-framework
Robot Testing Framework (RTF)
Stars: ✭ 12 (-95.26%)
Mutual labels:  unit-test
json2apex
Generate strongly typed apex code from a json structure.
Stars: ✭ 121 (-52.17%)
Mutual labels:  apex
mongo mock go example
How to mock MongoDB in Golang
Stars: ✭ 30 (-88.14%)
Mutual labels:  mock
mock-req-res
Extensible mock req / res objects for use in unit tests of Express controller and middleware functions.
Stars: ✭ 39 (-84.58%)
Mutual labels:  mock
phake
PHP Mocking Framework
Stars: ✭ 464 (+83.4%)
Mutual labels:  mock
DeepfakeHTTP
DeepfakeHTTP is a web server that uses HTTP dumps as a source for responses.
Stars: ✭ 373 (+47.43%)
Mutual labels:  mock
mokker
The mock does not mock you. The video: https://www.youtube.com/watch?v=gGLNJpC-Ov0
Stars: ✭ 13 (-94.86%)
Mutual labels:  mock
smart-cloud
基于springboot && springcloud的脚手架,支持服务合并部署与拆分部署、接口加解密签名、日志数据 脱敏、接口数据mock、接口文档自动生成、请求幂等校验、接口日志&&sql日志切面打印、分表分库分布式事务、国际化语言等
Stars: ✭ 167 (-33.99%)
Mutual labels:  mock
conan-gtest
[OBSOLETE] The recipe is now in https://github.com/conan-io/conan-center-index
Stars: ✭ 11 (-95.65%)
Mutual labels:  unit-test
cocoyaxi
A go-style coroutine library in C++11 and more.
Stars: ✭ 2,392 (+845.45%)
Mutual labels:  unit-test
kgb
Python function spy support for unit tests
Stars: ✭ 46 (-81.82%)
Mutual labels:  unit-test

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

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/interfacemocks.properties" "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);

Documentation

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].