function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Amit Kr SinghAmit Kr Singh 

how to write test class for consuming Third party rest API integration class

Hi Please help write test class and get code coverage for my class

public class IssuersCalloutController{

    public IssuersCalloutController(ApexPages.StandardController controller) {


public PageReference saveCalloutResult(){
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setHeader('Content-Type', 'application/json');
         system.debug('request endpoint '+ req);    
        String username = 'amit';
        String password = 'password';
        Blob headerValue= Blob.valueOf(username + ':' + password);
        system.debug('headerValue'+ headerValue);
        String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
        req.setHeader('Authorization', authorizationHeader);
        system.debug('###req### '+ req);            
        HttpResponse res = h.send(req);
        system.debug('###res### '+ res);
        string response = res.getBody();
        system.debug('response'+ response);
        Set<String> bankNames = New Set<String>();
        // declare issuer list to inser 
        List<Issuers__c> InsertedIssuers = New List<Issuers__c>();
        // I used limit in below query, but its better to use any filter to limit the data
        for(Issuers__c issuer : [Select Id,Bankdir_Name__c from Issuers__c LIMIT 50000]){
             //system.debug('###Bank Name### '+ bankNames); 
             //system.debug('issuerbankdir'+ issuer.Bankdir_Name__c);
        system.debug('###Bank Name### '+ bankNames.size());
        List<JsonParserDetail> responseDetails = JsonParserDetail.parse(response); 
        system.debug('****responseDetails****** '+responseDetails);
        if(responseDetails.size() > 0){
            for(JsonParserDetail jpd : responseDetails){
        If(InsertedIssuers.size() >0){
            insert InsertedIssuers;
        PageReference p = apexPages.currentPage();
      ApexPages.Message msg=new ApexPages.Message(ApexPages.Severity.CONFIRM,'Preview 2 list Successfully updated'+InsertedIssuers.size()+'"Thank you!');
      return p;
  public Issuers__c getIssuer(string iName, string ibName, string pName, string spName,string siloName){
        Issuers__c issuer = New Issuers__c();
        issuer.Name = iName;
        issuer.Bankdir_Name__c = ibName;
        issuer.Processor_Name__c = pName;
        issuer.SubProcessor_Name__c = spName;
        issuer.Silo_Name__c = siloName;
        return issuer;

Raj VakatiRaj Vakati
Step 1 :  Create a mock request . Provide an implementation for the HttpCalloutMock interface to specify the response sent in the respond method, which the Apex runtime calls to send a response for a callout.
global class MockHttpResponseGenerator implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {
        // Optionally, only send a mock response for a specific endpoint
        // and method.
        System.assertEquals('', req.getEndpoint());
        System.assertEquals('GET', req.getMethod());
        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        return res;

Step 2 : Call that mock Web service in apex test class
private class IssuersCalloutController{
     @isTest static void testCallout() {
        // Set mock callout class 
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
        IssuersCalloutController ic = new IssuersCalloutController() ;
        // Call method to test.
        // This causes a fake response to be sent
        // from the class that implements HttpCalloutMock. 

Hello Amit,

You can write the test mock class to do the call out as below
global class MockCallOutResponse implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest req) {
        System.assertNotEquals('https://test/getIssueList', req.getEndpoint());
        HttpResponse res = new HttpResponse();
        String body = strBody;
        return res;
    public static final string strBody = '{'+       
        '           "SerialNo": 1,'+
        '           "Name": "Preyanka",'+
        '           "RequestDate": "08/10/2018"'+
        '}';  //Replace this string with your own JSON format, I have just give a sample.

Call the above code from your test class of controller
public class IssuersCalloutController_Test {    
@isTest static void Controllertest(){
        IssuersCalloutController objController = new IssuersCalloutController();
        Test.setMock(HttpCalloutMock.class, new MockCallOutResponse());