You need to sign in to do that
Don't have an account?

Test coverage for http callout
Hi,
I have 3 http callouts in batch - execute method, i am writting test class for batch class. I have created Mock test class.
Problem is: i can able to cover first callout, but not 2nd and 3rd callout
Please tell me how many callouts we can test from test class, please advise me how can i do this
I have 3 http callouts in batch - execute method, i am writting test class for batch class. I have created Mock test class.
Problem is: i can able to cover first callout, but not 2nd and 3rd callout
Please tell me how many callouts we can test from test class, please advise me how can i do this
NOTE: Please use the "Add a code sample" button (icon <>) when adding code to increase readability and ease in referencing
<<<< === Batch Apex === >>>>
global class CalloutsClass implements Database.Batchable<Case>,Database.AllowsCallouts {
global Iterable<Case> start(Database.BatchableContext BC){
System.debug(' ====> Entered in to Start methods <==== ');
List<Case> cslst = [Select id, lastmodifieddate,caseid1__c,caseid2__c,caseid3__c status from Case];
return cslst;
}
global void execute(Database.BatchableContext BC, List<Case> scope){
System.debug(' ====> Entered in to execute methods <==== ');
List<Case> caseList = scope;
String baseUrl = 'https://test.company.net';
String systemId = '900';
String username = 'testusname';
String password = 'testpassword';
String objectType ='CASE';
String objectId;
if(caseList.size() > 0){
for(Case cs: caseList){
System.debug(' ====> Entered in to Case list iteration <===== ');
objectId = cs.id;
//Construct HTTP
HttpRequest req1 = new HttpRequest();
HttpResponse res1 = new HttpResponse();
Http http1 = new Http();
//Construct HTTP
HttpRequest req2 = new HttpRequest();
HttpResponse res2 = new HttpResponse();
Http http2 = new Http();
if(cs.Priority == 'Low'){
//Construct Authorization and Content header
Blob headerValue = Blob.valueOf(username+':'+password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
req1.setHeader('Authorization', authorizationHeader);
req1.setHeader('Content-Type','application/json');
String endpoint1;
endpoint1 = baseUrl+'/rest/api/2/issue/'+caseid2__c;
System.debug(' ===> End pint URL is: <=== ' + endpoint1);
req1.setMethod('PUT');
System.debug(' ===> 1 method PUT <=== ' + req1);
req1.setEndpoint(endpoint1);
System.debug(' ===> 2 endpoint <=== ' + req1);
req1.setBody('{"fields":{"customfield_test":"'+cs.id+'"}}');
}
if(cs.Priority == 'high' ){{
//Construct Authorization and Content header
Blob headerValue = Blob.valueOf(username+':'+password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
req2.setHeader('Authorization', authorizationHeader);
req2.setHeader('Content-Type','application/json');
//Constructing End-Point
String endpoint2;
endpoint2 = baseUrl+'/rest/api/2/issue/'+caseid2__c;
System.debug(' ===> End pint URL is: <=== ' + endpoint2);
//Set Method and Endpoint and Body
req2.setMethod('PUT');
System.debug(' ===> 2.1 method PUT <=== ' + req2);
req2.setEndpoint(endpoint2);
System.debug(' ===> 2.2 endpoint <=== ' + req2);
req2.setBody('{"fields":{"customfield_test":"null","customfield_test2":" ","customfield_test3":" ","customfield_test4":" ","customfield_test5":"
","customfield_test6":" "}}');
}
System.debug(' ===4 req body === ' + req2.getBody());
}
try {
System.debug(' ===> Entered into try block <=== ');
if(cs.Priority == 'Low' ){
res1 = http1.send(req1);
}
if(cs.Priority == 'high' ){{
res2 = http2.send(req2);
}
if(cs.Priority == 'Medium' )
{
//Construct HTTP request and response
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
//Construct Authorization and Content header
Blob headerValue = Blob.valueOf(username+':'+password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
req.setHeader('Content-Type','application/json');
//Constructing End-Point
String endpoint3;
endpoint3 = baseUrl+'/rest/api/2/issue/'+cs.case3id__c;
System.debug(' ===> End pint URL is: <=== ' + endpoint2);
//Set Method and Endpoint and Body
req2.setMethod('PUT');
System.debug(' ===> 2.1 method PUT <=== ' + req2);
req2.setEndpoint(endpoint2);
System.debug(' ===> 2.2 endpoint <=== ' + req2);
req.setBody('{"fields":{"customfield_test":"null","customfield_test2":" ","customfield_test3":" ","customfield_test4":"
","customfield_test5":" ","customfield_test6":" "}}');
res = http.send(req);
}
}
catch(System.CalloutException e) {
System.debug('ERROR:' + e);
}
}
}
}
=======================================================================>
=======================================================================>
<<<<< ======= Test Class ======= >>>>>>
@istest
public class testclassforcallouts
public static testmethod void testMain3(){
List<Case> caseList = new List<Case>();
Case cse2 = new Case(Origin = 'web',Subject = 'Sample Subject2', Priority = 'Low', Status='Open', caseid1__c = '647347jdfx', caseid2__c = '647347jdfx', caseid3__c =
'647347jdfx' );
insert cse2;
caseList.add(cse2);
Case cse3 = new Case(Origin = 'web',Subject = 'Sample Subject3', Priority = 'high', Status='Open', caseid1__c = '647347jdfx', caseid2__c = '647347jdfx',
caseid3__c = '647347jdfx');
insert cse3;
caseList.add(cse3);
Case cse4 = new Case(Origin = 'web',Subject = 'Sample Subject4', Priority = 'Medium', Status='Open',caseid1__c = '647347jdfx', caseid2__c = '647347jdfx',
caseid3__c = '647347jdfx');
insert cse4;
caseList.add(cse4);
String baseUrl = 'https://test.company.net';
String systemId = '900';
String username = 'testusname';
String password = 'testpassword';
String objectType ='CASE';
String objectId;
// Create an instance of batchable context
Database.BatchableContext BC;
// Set mock callout class
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGeneratorTest());
String baseUrl = 'https://test.company.net';
String systemId = '1';
String username = 'test.com';
String password = 'Ext100';
String objectType ='CASE';
String objectId;
CalloutsClass batch1 = new CalloutsClass();
batch1.start(BC);
batch1.execute(BC,caseList);
batch1.finish(BC);
}
=======================================================================>
=======================================================================>
<<<<<<======= Mock Class =============>>>>>>>>
@isTest
global class MockHttpResponseGeneratorTest 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('http://api.salesforce.com/foo/bar', req.getEndpoint());
System.assertEquals('GET', req.getMethod());
// Create a fake response
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{"foo":"bar"}');
res.setStatusCode(200);
return res;
}
}
Thanks in Advance!!!!
Please ignore this