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();
}
}
testMethod
is deprecated, have you faced the same issue annotating the test method with@isTest
?Test.setMock
is called after the call to the callout methodgetperformcallout
.setMock()
called after the async method is first called, but beforeTest.stopTest()
). The idea being that since the async code isn't actually run untilTest.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.