Write Mock SOQL Tests For External Objects

by Sudipta Deb | Sep 25, 2024 | Apex, Salesforce, Salesforce Winter 25 Release, test class, unit testing | 0 comments

Sudipta Deb

Sudipta Deb

Founder of Technical Potpourri, Co-Founder of Shrey Tech, Enterprise Cloud Architect

Salesforce Winter '25 Release is here. I have published few dedicated posts and videos covering new features from Winter'25 release. If you haven't checked them out, please go ahead and check them below.

In today's blog post, I will share a new Apex feature through which you can write Mock SOQL Tests for External Objects. Please provide your feedback in the comment, and don't forget to subscribe to my youtube channel and blogpot.

Watch The Video Or Read The Article (Or Do Both smile)

The form you have selected does not exist.

What Is External Object?

External Objects are a powerful feature in Salesforce that allows you to connect to external data sources and treat them as if they were native objects within your Salesforce org. This means you can create records, update fields, and query data from these external sources directly from Salesforce.

Key Benefits:

  • Seamless Integration: External Objects eliminate the need for complex integrations or APIs to access external data.
  • Enhanced Data Management: You can manage and manipulate external data within your familiar Salesforce environment.
  • Improved Workflow Automation: Automate processes based on external data changes, streamlining your business operations.
  • Enhanced Reporting and Analytics: Gain insights from a unified view of your internal and external data.

Problem Statement

When we write any Apex class which involves some sort DML operation around the External object, we need to write the test classes as well. Writing test class is needed to make sure atleast 75% of the code is covered in the test class with both positive, negative and bulkified scenarios. In case of SOQL statements, we normally create the test data in the test setup method and query them in the test methods. But in case of SOQL which involves External object, we can not do that and also we can not directly query the External objects. But test coverage is needed for the Apex class to be deployed in Production. 

In Winter 25 Release, Salesforce brings the solution to this problem. Now we can write mock SOQL statements for External Objects. I will be showing that in this blog.

Quick Setup Overview

In my Development Org, I have configures the External Data Source and from the data source, I have synced two objects - Order and OrderDetails. Both are External Objects. The way to identify external object is if you go to Setup and search for External object, you will find all external objects under the sub-menu External Objects. And External Objects always ends with __e and not like custom objects which always ends with __c. In this blog post, I will be mainly dealing with Order External Object.

Apex Class Involving Order External Object

Here is the apex class which has a method where I am fetching 10 Order records and returning back to the calling method. Very basic apex class. But we need test coverage for this class. That's what I will be writing now.

Source: Github Repo

Stub Provider Class

Below is the class which provides the mock soql data. The class must exten SoqlStubProvider and must override the method handleSoqlQuery().

Source: Github Repo

Test Class

Here comes the test class. When the test class will execute the fetchAllOrders() method, then instead of querying External object, system will execute the Stub Provider class i.e. OrderProvider in test context.

Source: Github Repo

Final Thoughts

That's all for today. I hope with this blog post and youtube video, you will be able to figure out how to write test class which includes mock soql for your external objects. In next couple of days, I will bring more Winter 25 features in this blog as well as my youtube channel. So it is highly recommended that you please subscribe to my channel and blog to come to know about all the nice features. Thank you!!

Disclaimer

This article is not endorsed by Salesforce, Google, or any other company in any way. I shared my knowledge on this topic in this blog post. Please always refer to Official Documentation for the latest information.

0 Comments

Leave a Reply

Written by Sudipta Deb

Enterprise Cloud Architect, Content Creator, 20x Salesforce Certified, 1x Google Cloud Certified, 2x Copado Certified

Related Posts

Salesforce Cancels the “Permissions in Profiles” Retirement: What It Means for Admins and Architects

Salesforce Cancels the “Permissions in Profiles” Retirement: What It Means for Admins and Architects

For the last few years, Salesforce administrators, architects, and security teams have been preparing for a major platform change: the retirement of permissions managed directly through Profiles. Salesforce had previously announced plans to move organizations toward a Permission Set–led security model, with enforcement expected to begin around the Spring ’26 timeframe.

read more...

0 Comments

Leave a Reply