SFDC Stop - Always the latest about Salesforce


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


Telegram logo   Join our Telegram Channel

Thursday, 21 November 2019

Salesforce Integration Tutorial Part 7 - Creating a test class for Apex Web Service

Hello Trailblazers,

From part 1 to 6 in the Salesforce Integration Tutorial Series, we created custom APIs in salesforce and also learned about the various annotations for different HTTP methods including:- GET, POST, DELETE, PUT and PATCH method.

In this tutorial, you'll learn, how we can create a test class for an apex web service. I have added all the methods together in my ContactResource class available in the master branch of my github repository. You can have a look at the same here.

I created a test class for this ContactResource class named ContactResourceTest which is given below:-

As you can see above, I have a total of 7 methods in the above test class out of which I'll be explaining about:- testGetContactIdAndNames, testCreateNewContact and testUpdateContact. Rest of the methods are similar to these and if you're still not able to understand any of those, let me know in the comments. Let's begin.

makeData()

Before moving onto our methods, I just want you to brief about the makeData() method which is doing nothing but inserting a new contact with name:- Richard Hendricks. This method will be called automatically before all the other methods when the test class is executed because it's annotated with @TestSetup annotation. The test setup method is mainly used to create data which is required by the test class so that the test class can run properly.

testGetContactIdAndNames()

In this method, first of all I have created an instance of RestRequest class named:- contactRequest. I have setup the requestUri to the actual URL of my salesforce instance that I am going to hit. This URL consists of 1 at the end as you know that in my GET request to the /ContactAPI/, I need to specify the number of contacts I am going to fetch at the end of URL, so, I have specified it as 1. Then, I've setup the HTTP method as GET in the request as my actual class method getContactIdAndNames() has an @HTTPGet annotation. I have finally assigned this contactRequest to the request variable of RestContext class to set the input request before calling the actual method. Then, I called the getContactIdAndNames() method from the ContactResource class which returned a list of contacts in the response that I stored in returnedContacts variable. Finally, I am confirming the reponse by checking that the returnedContacts list is not null, has a single contact that I inserted in the makeData() method and has the same name that I gave it to during the insertion of the contact.

testCreateNewContact()

In this method, we're simply calling the createNewContact() method from the ContactResource class. This will work as we're able to pass the required data through the method parameters and we've not checked anything else (For ex:- HTTP method of request, URL etc.) in the actual method. So, we don't need to setup a rest request to test this method. As we know, our createNewContact method will create a new contact. Therefore, I'll query the newly created contact by using the id from the contact which is returned by our method. For confirmation, I'll check that the returned contact is not null and has a valid id, as well as I'll check that the newly created contact is not null and has the same lead source which was in the returned contact.

testUpdateContact()

In this method, first of all I have queried a single contact to get the id of the contact which is already inserted. Then, I have created a new instance of Contact and set it's first name. We're going to send this contact in the request body and will update our existing contact's first name. Next, I created a new instance of RestRequest and setup the httpMethod as PATCH and the requestUri with the proper value. You can see that I have added the queried contact's id at the end of request uri as if you remember our updateContact() method, it'll get the id of the contact to be updated from the URL and the new contact parameters from the request body and will then update the contact and return the same in the response. Next, I've set the requestBody variable of contactRequest. It should have the type as BLOB, so, I have converted my contact object into JSON using JSON.serialize() and then converted the JSON string to blob using Blob.valueOf(). I have set this contactRequest to the RestContext.request so that my request is setup properly. Then, I called my updateContact() method from my ContactResource class and queried the contact again with the contact id that I got in the initial query.
Finally, I checked that the returned contact in the response and the queried contact have the same id. I also checked that the first name is updated to the new value according to my request body and the last name is the same as before.

Tired of reading or just scrolled down, don't worry, you can watch the video too.



So, in this tutorial, you learned how you can create a test class for apex web services, setup HTTP request context variable using RestRequest class and also calling the method directly by passing the parameters if it's not necessary to setup request.

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

Happy Trailblazing..!!

2 comments:

  1. Great thanks 🙏 Rahul. You are doing great job. Please post the series on soap web services as well.

    ReplyDelete
    Replies
    1. Thank you :-) It's in the plan will post about SOAP after the rest callouts section is complete.

      Delete