Salesforce SOQL-Free Apex Tests with Selector Pattern

With Apex a Selector Pattern provides multiple benefits including

  • A centralized place to query once, minimizing the number of queries
  • The ability to test the queries in the Selector independent from the classes that utilize the Selector

Selector Patterns can allow for a configurable query. In the case below we configure with Custom Metadata

As indicated create an interface

The below trigger handler class does not do anything functional but it is an example of how we can inject a Mock of the selector into the constructor. This allows query-free dml-free tests on the class.

In the test, we can Mock the selection of data by creating a Mock class and generating the data we care about in the Mock.

The test injects a selector mock now allowing for SOQL-free apex tests. The Selector pattern limits SOQL to tests that test the selector patterns, allowing us to scale with tests that don’t need awkward workaround to queries.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store