In the previous post Salesforce Rest API Integration Part 2: Parsing JSON In Salesforce we have successfully completed our development to fetch user info from Random user and displaying the same in Visual force page.
In this post we will be carrying out the Apex test class development for the same.
STEP 1: Create Mock Response
For Testing Rest Services we have to first setup Mock Responses class which will give us sample responses as expected by the main class. For this we will implement the standard HttpCalloutMock interface as below.
@isTest |
STEP 2: Create Test Class
Create a test class by using Test.setmock to redirect all the requests from here to the mock response generator class we have created above.
@isTest |
As noted in the previous post JSON2Apex is an amazing tool which generates both Apex class and test class. Have a look at the same.
This brings out the end of how to consume rest API services in Apex, what we will see next is how to expose Rest Services in Apex