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

Friday, 31 January 2020

Salesforce Integration Tutorial Part 11 - Test class for SOAP API Callout

Hello Trailblazers,

Welcome to the 11th tutorial of the Salesforce Integration Tutorial series. In the previous tutorial, we performed a callout to a SOAP API by importing a WSDL file in Salesforce. You can find that tutorial by clicking here. In this tutorial, we're mainly going to create a test class for the same callout that we performed before. So, make sure you've completed the previous tutorial before you begin with this one.

If you remember, in case of REST APIs, when we have to create a test class for an api callout, we used to create a mock class that implements HTTPCalloutMock interface and that mock class was used to create a mock response for the API callout that is going to initiate from our test class. In this case, we're going to do the same.

This time, we're going to create a mock class and implement a different interface which is known as WebServiceMock. This interface consists of a doInvoke() method which we need to override in our mock class. The doInvoke() method will receive request and response in the parameters and we can set the response parameter with the fake response that we're going to send for our soap api callout while running a test class.

If you remember, we named our auto-generated class for SOAP API callout as CalculatorService. So, let's name our mock callout class as:- CalculatorServiceMock. The code for the mock class is given below:-

As you can see above, our calculator service soap api basically peforms 4 mathematical operations:- Addition, Subtraction, Multiplication and Division. So, we've setup 4 macros named as:- ADD_MODE, SUB_MODE, MUL_MODE, DIV_MODE. These 4 macros basically define 4 modes in which our mock class will run to generate different responses when different methods of CalculatorService class are called. We've also declared another string variable named as mode which is setup using a constructor of this mock class.

Inside our doInvoke() method, we're checking the mode of our callout and setting up the fake response by using the responsewrapper provided by our auto-generated calculatorservice class.For ex:- When we have to generate a response for addition, we're using AddResponse_element response wrapper from CalculatorService class to setup the result of addition and finally setting up the response using the response map in which we have the key as:- response_x and the value as the instance of AddResponse_element class which is addResponse. Each response wrapper has a result variable like:- AddResult in case of addition and SubtractResult in case of subtraction where we can set the result of operation.

We've setup the result of operations accordingly by assuming that our input elements are:- 6 and 3. We'll be sending these numbers from our test class to the api callout so that we get the correct response from the mock.

Now, it's time to create our test class for SOAP API callout. We've created a test class with name:- CalculatorServiceTest. The code for the same is given below:-

As you can see above, first of all we've setup two integers as x and y with values 6 and 3 as we know that our mock class will send the correct response for any operation that we call using the soap api for these inputs. After that we've 4 test methods defined, one for each operation i.e.:- Addition, Subtraction, Multiplication and Division that can be performed by our CalculatorService class. So, we basically need to cover the Add(), Subtract(), Multiply() and Divide() methods which are defined in the CalculatorSoap inner class of our CalculatorService class that we were using to make callouts in our previous tutorial.

Let's have a look at testAddCallout() method. In this method, first of all we set the mock using our Test.setMock() method. This time we pass the instance of WebServiceMock.class in the first parameter and an instance of our CalculatorServiceMock class in the second parameter with the mode as CalculatorServiceMock.ADD_MODE in the constructor. Remember, ADD_MODE was one of the macros that were defined in our mock class and is used to check the mode in order to define the addition response. That's why we're passing the same variable in the constructor to setup the correct response for our additon SOAP callout.

After that, we simply created an instance of our CalculatorService.CalculatorSoap class named as calculator. We called the Add() method and passed our x and y variables with values:- 6 and 3. You can checkout the mock class again to see that we've setup the mock response as 9 in AddResult which is the sum of 6 and 3. We got the result from add method i.e. the actual api callout and that result is stored in result integer variable. We've also setup another variable expectedResult which is x+y. So, this expectedResult also has a value 9 and the result variable also receives 9 from our mock api. At last, we check that our expectedResult and result variables have same value using System.assertEquals() method and our test method for addition is complete.

Similarly, we've made different methods to cover subtraction, multiplication and division operations in our test class. You can run this test class and you'll see that the CalculatorService class is covered with 100% coverage as shown below:-


Congratulations..!! You've successfully created a test class for SOAP API callout with 100% coverage.

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



All the code for this tutorial is present in our soapcallouttest branch of the SFDC-Integration-Tutorial github repository that can be accessed by clicking here. That's all for this tutorial, if you liked it, do share it in your network and let me know your feedback in the comments section down below.

Happy Trailblazing..!!

No comments:

Post a comment