Unit Test: Can we mock relationships in Salesforce Apex?
Tags: apex, salesforce, dev, testing
Published: 2018-04-24 04:38:12

---
title: Unit Test: Can we mock relationships in Salesforce Apex?
published: true
cover_image: https://thepracticaldev.s3.amazonaws.com/i/xxbtp0jo5oeny4b3pu0n.jpg
description: A technical article about a Data Layer pattern to mock records relationships in Apex. It's very useful when you need to improve tests performance. 
tags: #apex #Salesforce #dev #testing
---


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 <a href="https://salesforce.stackexchange.com" rel="noopener" target="_blank">Salesforce Stack Exchange</a>. 

And a few days ago someone posted the <a href="https://salesforce.stackexchange.com/questions/215326/can-we-mock-relationships-in-apex/" rel="noopener" target="_blank">following question</a>: 

<blockquote><em>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?</em></blockquote>

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

And the answer is <strong>yes, it's possible</strong>. 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 <a href="https://salesforce.stackexchange.com/questions/215326/can-we-mock-relationships-in-apex/" rel="noopener" target="_blank">in that question</a>, you could add an Interface to your main class. Let's call it IDataLayer. Add this new interface to the main class (here, called <strong>DataLayerHandler.cls</strong>)

{% gist https://gist.github.com/toadgeek/90fbc41ec028e270d11825f6d114b399 %}

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

{% gist https://gist.github.com/toadgeek/b9fc466354aef522c89d0f64627fb2c6 %}

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:

{% gist https://gist.github.com/toadgeek/e959493d6a5507b235f8beb6ad01fe92 %}

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:

{% gist https://gist.github.com/toadgeek/44a001492c775b53a3b914b0ba9b5aac %}

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

{% gist https://gist.github.com/toadgeek/285e7826f302f24e73f55843f4239ddd %}

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!