0

This is my controller class.

public with sharing class RSLeadReportcontroller {

    public List<RSLeadReport> ConsoleWrapperList{get;set;}
    
    public List<RSLeadReport> getperformcallout(){
    
        ConsoleWrapperList = new List<RSLeadReport>();
        
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        req.setTimeout(60000);
        
        String authorization= String.valueOf([SELECT Authentication__c FROM RS_External_Endpoint__mdt WHERE DeveloperName = 'RS_Token' LIMIT 1].Authentication__c);   
        
        req.setHeader('Authorization', 'Bearer '+authorization);
        req.setHeader('Content-Type', 'application/json');
        req.setEndpoint('https://rsdv-dt-loc-na-api.exxonmobil.com/api/v2/search/get/leads');
        req.setMethod('GET');
        
        res = http.send(req);
        
        if(res.getstatusCode() == 200 && res.getbody() != null){
        
            ConsoleWrapperList=(List<RSLeadReport>)json.deserialize(res.getbody(),List<RSLeadReport>.class);
        
        }
            
        return consolewrapperlist;
   }
        
    public PageReference exportToExcel(){
        PageReference retURLExcel = new PageReference('/apex/WebApp_Leads_excel');
        return retURLExcel;
    } 
          
}

And this is my test class.

@isTest
public class Test_RSLeadReportcontroller{
 
  @testSetup
  public static void setupTestData(){
    Test.startTest();
    Lead lead_Obj = new Lead(LastName = 'LastName885', FirstName = 'First641', MiddleName = 'Middl705', Company = 'Company493', Street = 'PratapNagar', City = 'Miami', State = 'Florida', PostalCode = '302022', Country = 'USA', MobilePhone = '54343-63122', Email = 'Email28@test.com', Website = 'http://test29.com', Status = 'New', Webapp_Lead_ID__c = 'Webap415', Lead_Pardot__c = false);
    Insert lead_Obj; 
    Test.stopTest();
  }
  
  public class RSCallout implements HttpCalloutMock{

            public HTTPResponse respond(HTTPRequest request) {
                // Create a fake response
                HttpResponse response = new HttpResponse();
                response.setHeader('Content-Type', 'application/json');
                response.setBody('{"data": {"identity": {"accountGroup": {"code": "0001"},"salesforceId": "0014100001eCl2U", "tradingName": "CPAC ROOF TILE" }}}');
                response.setStatusCode(200);
                //response.setStatus('Customer Found');
                return response;
            }        
  } 

  public static testMethod void test_exportToExcel_UseCase1(){
    List<Lead> lead_Obj  =  [SELECT LastName,FirstName,MiddleName,Company,Street,City,State,PostalCode,Country,MobilePhone,Email,Website,Status,Webapp_Lead_ID__c from Lead];
    System.assertEquals(true,lead_Obj.size()>0);
    RSLeadReportcontroller obj01 = new RSLeadReportcontroller();
    obj01.ConsoleWrapperList = new List<RSLeadReport>();
    obj01.exportToExcel();
  }

  public static testMethod void test_getperformcallout_UseCase1(){
    Test.startTest();
    List<Lead> lead_Obj  =  [SELECT LastName,FirstName,MiddleName,Company,Street,City,State,PostalCode,Country,MobilePhone,Email,Website,Status,Webapp_Lead_ID__c from Lead];
    System.assertEquals(true,lead_Obj.size()>0);
    
    RSLeadReportcontroller obj01 = new RSLeadReportcontroller();
    obj01.ConsoleWrapperList = new List<RSLeadReport>();
    obj01.getperformcallout();
    Test.setMock(HttpCalloutMock.class, new RSCallout());
    Test.stopTest();  
  }
             
}
5
  • 1
    testMethod is deprecated, have you faced the same issue annotating the test method with @isTest? Sep 1, 2020 at 20:03
  • @JefersonChaves Derek's answer is correct, but this is a good point. They should fix that problem.
    – sfdcfox
    Sep 1, 2020 at 20:04
  • Fair enough @sfdcfox, I also could not understand how this is supposed to work if the Test.setMock is called after the call to the callout method getperformcallout. Sep 1, 2020 at 20:06
  • 1
    @JefersonChaves The only way I can think that it might work is if the callout were in a future/queueable/scheduleable/batch class (with setMock() called after the async method is first called, but before Test.stopTest()). The idea being that since the async code isn't actually run until Test.stopTest() is executed, the mock is technically still set before the code that needs it is executed. Even if that does work, I don't think it'd be something anyone should recommend.
    – Derek F
    Sep 1, 2020 at 22:02
  • @DerekF: thanks a million for all the clarification, when I wrote the comments I was not seeing your answer, yet. Sep 2, 2020 at 8:57

1 Answer 1

3

You need to call Test.setMock() prior to running the code that is trying to make the callout, not afterwords.

Calling that method is what causes Salesforce to substitute the real callout for the mock one that you're providing (when the callout is attempted).

1
  • 1
    +1 though, according to the docs, you should call Test.setMock after Test.startTest as well.
    – sfdcfox
    Sep 1, 2020 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.