Code Monkey home page Code Monkey logo

fflib-apex-mocks's Introduction

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

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

fflib-apex-mocks's People

Contributors

dfruddffdc avatar imjohnmdaniel avatar afawcett avatar daveerickson avatar claychipps avatar cropredyhelix avatar daveespo avatar xapfrom avatar phardakerffdc avatar chazwatkins avatar dfrudd avatar xonoxforce avatar peterlin888 avatar stohn777 avatar dbtavernerffdc avatar chernyy-svyat avatar emcdaniel-sungage avatar blackbaud-craigpemberton avatar afawcettffdc avatar jthemphill avatar kjonescertinia avatar wimvelzeboer avatar kooltra-yizhang avatar evanmcd avatar sbresin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.