I am working on a project in which we need to test an APEX REST Service in such a way that no actual data is created in Salesforce during the testcases execution. I am new to APEX Development.

Searching on the web took me to fflib-apex-mocks library which seems to serve the purpose of avoiding the usage of actual test data inserted in the database for testcases execution. I have installed fflib-apex-mocks and fflib-apex-common in my sandbox environment.

So far, I am not able to find out a step-by-step guide as what I need to do if I want to test my existing Salesforce Objects at a unit level and how can I test my APEX Rest Service? From looking at the fflib-apex-common-samplecode it seems that I need to define classes for my Salesforce objects, I am not sure if I am getting this right.

At a higher-level, below are my questions at a higher-level:

  • I have a Student__c Object already defined in Salesforce, it has some triggers for insert and update which has checks for various fields and I want these triggers to fire when I try an insert or update for the mock object. I want to develop testcases for that, what do I need to do? How do I start?

  • I have an APEX Rest Service which gets a collection of Teacher__c objects converted to JSON based on the id of a Student__c object provided to it as a query parameter. I have previously developed testcases for APEX Rest Service but that didn't involved actual objects usage but my current scenario is different as it involves usage of actual objects.

Can someone please help me out in getting started?

  • Just addressing your first paragraph and ignoring the mocking framework. Any data that you create as part of a test case won't be persisted outside that test context. It's all gone once the test is complete. So in that sense, there is no need to a mocking framework. Commented Apr 4, 2016 at 20:07
  • Mocking the Apex REST service can be handled by the system using HttpCalloutMock - See Testing HTTP Callouts by Implementing the HttpCalloutMock Interface Commented Apr 4, 2016 at 20:08

1 Answer 1


I'm one of the developers who looks after ApexLib and ApexMocks.

We have a sample app to demonstrate using Apexlib patterns and how it helps with mocking dependencies. You can mock out the classes that deal with the database to write 'true' unit tests. These will run faster because you don't have the overhead of making a trip to/from the database, and also more robust because you don't need to satisfy object level validation.

That said, both previous posters are correct - you can mock the callout using HttpCalloutMock, and data created in a test doesn't leak into the production code and is automatically cleared up when your tests finish.

I'll still attempt to answer your question. Take a look at this production code: OpportunitiesServiceImpl

And this test: OpportunitiesServiceTest

OpportunitiesServiceImpl.applyDiscounts needs to do three things:

  1. Retrieve the correct Opportunity SObject records
  2. Process the SObject records (to apply a discount)
  3. Save the records

Retrieving the records is delegated to an OpportunitiesSelector, which is mocked out in the test. Saving the records is delegated to an fflib_SObjectUnitOfWork, which is mocked out in the test.

So the test can concentrate on the actual purpose of the method - which is to process the records in some way. We can assume that the correct records are supplied and the records are saved in this test - the selector and the unit of work have their own unit tests to verify this behaviour.

  • Thanks, @frup42 for your comments. Those were really helpful. My implementation was not layered properly which I have done now to make things easier for me. However, one of my service methods is issuing 3-4 DML statements and return a result by combining those. Is there a way to Mock DML statement responses? Please suggest. Commented Apr 12, 2016 at 22:16
  • I have a service class with the name of TeacherService which contains implementation of some methods like getAllTeachers(), getTeacherById(Id teacherId) etc. The TeacherService is a concrete class and I want to mock some of its methods, is it possible to do so? Please see the sample code here. Commented Apr 13, 2016 at 21:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .