Presented by Matheus Gonçalves
Originally presented at Salesforce Developer Group, Tampa, United States.
Files used in this exploratory presentation:
force-app
main
default
classes
◦ AccountTriggerHandler.cls
◦ DataLayerHandler.cls
◦ DocumentHelper.cls
◦ DocumentHelperTest.cls
◦ DocumentHelperWithDataLayer
◦ DocumentHelperWithDataLayerTest
◦ TestDataFactory.cls
◦ TestUtils.cls
triggers
◦ AccountTrigger.trigger
Apex tests are essential to the overall health of your Salesforce org. The Apex testing framework enables you to write and execute tests for your Apex classes and triggers on the Lightning Platform. Apex unit tests ensure high quality for your Apex code and let you meet the requirements for deploying Apex.
It's very common to have a Test Factory for your Apex tests, creating several records, which is absolutely needed especially when testing bulk processing operations.
However, besides bulk testing, you may want to test a singular method from a Helper class or run your tests with fake data. Here, instead of inserting real records in your Salesforce org, you can use the Data Layer pattern, and implement an interface that will allow you to run tests with mock data, which makes your tests run a lot faster.
You can mock virtually any relationship if you use a Mock Data Layer Pattern, by adding an Interface
to your Helper class. Let’s call it IDataLayer
.
Add this new Interface to the Helper class (here, called DocumentHelperDataLayer.cls
).
More details at matheus.dev.