SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.

Monday, 9 December 2019

Salesforce Integration Tutorial Part 9 - Test class for Apex REST Callout

Hello Trailblazers,

Welcome to the 9th tutorial of the Salesforce Integration Tutorial series. In the previous tutorial, we performed a callout to an external REST API and learned about the concept of wrapper classes and remote site settings. In this tutorial, we're going to create a test class for our api callout. I have made a small update in our SFDCStopCallout class in which I returned the response which I got from the API and updated the return type of getBlogs() method to HTTPResponse. You can have a look at the updated SFDCStopCallout class below:-

As shown above, we've the updated class and we're going to create a test class for this API callout. Let's begin.

Create a Mock Class

The first step is to create a mock class for API callout. The mock class is nothing but a class that will generate a fake response for our API callout in the test class. As, we don't need to hit the actual API while testing, we should have a fake response that will be returned when a callout is performed. I created a mock class for my SFDCStop Blogs API named as SFDCStopMock:-

As you can see above, the mock class is also a test class with @isTest annotation that implements HTTPCalloutMock interface. This interface consist of a single method respond() which accepts an instance of HttpRequest class as a parameter and return an instance of HttpResponse which we'll construct inside the method itself. So, we've overriden this respond() method inside which we initialized a new instance of HttpResponse named as response. Then we set the fake response body using the setBody() method and we also set the fake response header with key as Content-Type and value as application/json using the setHeader() method. Finally, we set the status code of 200 for the response using setStatusCode() method. At the end, we returned the fake response.

Test class for API Callout

Now, we have our mock API. So, we're ready to create a test class for our SFDCStopCallout class. I have created a new class named SFDCStopCalloutTest which is given below:-

As you can see in the above test class, I have created some constants that are given below along with their purpose:-

  1. RESPONSE_CODE :- This constant consist of the response code of the response coming from API callout.
  2. RESPONSE_HEADER_KEY :- This constant consist of the key in the response header from API callout.
  3. RESPONSE_HEADER_VALUE :- This constant consist of the value in the response header from API callout.
  4. RESPONSE_BODY :- This constant consist of the body of the response from API callout.

All these constants are used in test methods to verify the response coming from the api callouts in the assert methods. As you can see, I have created two test methods:- testGetBlogs and tesGetBlogsUsingFramework. As both the methods are covering the same getBlogs method of SFDCStopCallout class, we're going to have a look at the testGetBlogs method first which is the standard approach.

Inside this method, I have first setup the mock API using Test.setMock() method in which the first parameter is the interface type which is always:- HTTPCalloutMock.class and the second parameter is the object of our mock class. Therefore, I have passed it as new SFDCStopMock() as my mock class is SFDCStopMock which will return a correct fake response for this callout. In the next line, I am performing the API callout by calling the getBlogs() method from SFDCStopCallout class. This method is returning an instance of HTTPResponse that I am storing in a variable named response. Finally, I am verifying the response code, response header and the response body which is returned by our callout method by using System.assertEquals() method.

You'll get 100% coverage of the SFDCStopCallout class using this testGetBlogs() method alone. Now, one question to consider is:-

Do I need to create mock callout classes for each callout ?

The answer is YES. You need to create a mock for each callout you're perfoming in your apex to have full test coverage of that callout. However, you can use the same callout class by adding some conditions to provide different responses for different callouts. The easy solution to this problem is given in our second method which is testGetBlogsUsingFramework().

testGetBlogsUsingFramework() is making use of HTTPCalloutServiceMock class present in HTTPCalloutFramework to test this callout. HTTPCalloutFramework is a framework that simplifies HTTP Callouts in apex. You can install it from the GitHub repository in one click by clicking on the Deploy to Salesforce button. Click this link to go to the repository and click on the button to install the framework.

Now let's have a look at the second method that's using the framework. In this method, first of all I created an instance of HTTPCalloutServiceMock class named as sfdcStopApiMock. Then I set the response code, response header and the response body of my fake response by passing the constants as the parameters in the the setter methods available in the class. In the Test.setMock() method, I have passed the instance of HTTPCalloutServiceMock i.e. sfdcStopApiMock as the second parameter. The rest of the code is the same as in the first method. You can test the SFDCStopCallout class with this second method alone once you have installed the framework and it'll give you 100% coverage. So, we got to a conclusion that by using the framework,

"We are doing the same work performed by creating a separate mock class that is perfomed by writing 4 lines of code using HTTPCalloutFramework"

And the good thing is, you can reuse the same framework class again and again for different callouts. So, it's a great time saver for any developer. Similar is the case while calling out using the framework. If you want to have a detailed look on the framework you can view another blog post by clicking here.

That's all for this tutorial. If you liked it, make sure to share it among your network and let me know your feedback in the comments section below.

Happy Trailblazing..!!

2 comments:

  1. Amazing blog for better API understanding and content is very useful !! Great job

    ReplyDelete
    Replies
    1. Happy to see that you liked it Sonam :-) Make sure to share it among others too...!!

      Delete