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

Saturday 15 February 2020

Salesforce Integration Tutorial Part 13 - Creating a Test Class for a SOAP Web Service in Apex

Hello Trailblazers,

Welcome to the 13th tutorial of the Salesforce Integration Tutorial Series. In the previous tutorial, we created a SOAP web service in apex. You can have a look at that tutorial by clicking here. In this tutorial, we're going to create a test class for a SOAP web service.

Let's have a look at the below code:-

As  you can see above, we have a webservice method in our ContactResource class. This method takes an integer which is the limit of contacts. Then, we query some contacts from our org using that limit and return the list of contacts in the response.

Now it's time to create a test class for the same. The test class for this web service will be exactly the same as we make a test class for a normal apex class. We just have a method which is accepting an integer as a parameter and returning a list of contacts. I created a new test class for our ContactResource class named as ContactResourceTest. Let's have a look at the code below:-

As you can see above, we have a TestSetup method which is creating 5 contacts with name Rahul Malhotra. This is our test data. Then, we have a testGetContactIdAndNames method which is simply calling the getContactIdAndNames method of our ContactResource class by giving the integer 1 in the parameter. That method is returning a list of contacts which we have stored in returnedContacts variable. Finally, we're just checking that our returnedContacts list is not null, has a size of 1 and the contact inside that list has a name of Rahul Malhotra. So, we just checked that our method is working as expected and when you'll run this test class, you'll find that our ContactResource class is covered with 100% coverage as shown below:-


That's all for this tutorial. You have successfully created a test class for a SOAP Web Service class in apex. You can find the code for the web service and test class in the soapapi branch of my SFDC-Integration-Tutorial github repository by clicking here.

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



Do checkout the other tutorials in this whole integration tutorial series by clicking here. If you liked this tutorial, make sure to share it in your network and let me know your feedback in the comments section down below.

Happy Trailblazing..!!

1 comment: