loading...
Cover image for Unit Test: Can we mock relationships in Salesforce Apex?

Unit Test: Can we mock relationships in Salesforce Apex?

toadgeek profile image Matheus Goncalves ・2 min read

I've been trying to use some of my spare time to help my fellow Salesforce Developers in our community. And one of the best places to do so, is Salesforce Stack Exchange.

And a few days ago someone posted the following question:

How can I write a test method without actually doing the query (to speed up test run times)? As far as I know, the only way to return relationships more than one level deep is to query for them. I know ApexMock has IDGenerator but cannot see how I can use it for this use case?

Well, can we mock relationships in Apex to improve a test's performance?

And the answer is yes, it's possible. I'm going to explain how to achieve that in incremental steps, to try to simplify the concept.

You can mock virtually any relationship if you use a Mock Data Layer Pattern.

Imagining the scenario presented by the developer in that question, you could add an Interface to your main class. Let's call it IDataLayer. Add this new interface to the main class (here, called DataLayerHandler.cls)

Now, in the same class, let's create an inner class for this data layer, implementing the Interface you just created:

Ok, now how to access it? We need to create an instance of this DataLayer, and we will use the DataLayerHandler Class constructor for that. This is really important, I'll explain it below:

But why to do all of that?! And this is the magic: because now you can pass a MOCK version of the data layer to you class to use!! Given this handler class, let's create a test class and a mock class, implementing the same interface:

With the mock version of the data layer, you can start creating your mock records. Use the method getFakeId(Schema.SObjectType type) to create Ids to your mock records:

There you go. This way you can mock several different types of relationships.

This is an extremely useful pattern and allow you to test your logic in different scenarios without running queries, without hitting the database. It's a good way to improve your tests performance.

Just don't forget to create some bulkified tests as well.

I hope that helps!

Posted on by:

toadgeek profile

Matheus Goncalves

@toadgeek

Lead Software Engineer, podcaster at https://toadcast.com.br, drummer, photographer. Neutral Good Geek. Salesforce Certified Developer ☁ and Lead Software Engineer at Salesforce.

Discussion

pic
Editor guide