You need to sign in to do that
Don't have an account?
Sanchi9
Test class for Batch callout
Hi, I want to write a test class for Batch Callout. The code is below:
global class BatchSync implements Database.Batchable<sObject>, Database.AllowsCallouts{
global Database.QueryLocator start(Database.BatchableContext bc){
String query = 'SELECT id, Name, Type From Account WHERE Type='Prospect';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Account> scope){
for (Account acc : scope){
try{
HttpRequest request = new HttpRequest();
HttpResponse response = new HttpResponse();
Http http = new Http();
String username = 'Username';
String password = 'Password';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
request.setHeader('Authorization', authorizationHeader);
request.setHeader('Content-Type', 'application/json');
request.setEndpoint(enpointurl);
request.setMethod('GET');
response = http.send(request);
if (response.getStatusCode() == 200) {
String jsonResponse = response.getBody(); acc.Type='Customer';
}
JSONParser parser = JSON.createParser(response.getBody());
HttpResponse res = http.send(request);
//list of header names keys
string[] headerkeys = res.getHeaderKeys();
Map<string, string> headers = new map<string, string>();
//iterate through they keys, and populate your map
for(string s : headerkeys){
headers.put(s,res.getHeader(s));
system.debug('header: ' + s + ' value: ' + res.getHeader(s));
}
}
catch(Exception e){
System.debug('Error-' + e.getMessage());
}
update scope;
}
}
}
global void finish(Database.BatchableContext bc)
{
}
}
global class BatchSync implements Database.Batchable<sObject>, Database.AllowsCallouts{
global Database.QueryLocator start(Database.BatchableContext bc){
String query = 'SELECT id, Name, Type From Account WHERE Type='Prospect';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Account> scope){
for (Account acc : scope){
try{
HttpRequest request = new HttpRequest();
HttpResponse response = new HttpResponse();
Http http = new Http();
String username = 'Username';
String password = 'Password';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
request.setHeader('Authorization', authorizationHeader);
request.setHeader('Content-Type', 'application/json');
request.setEndpoint(enpointurl);
request.setMethod('GET');
response = http.send(request);
if (response.getStatusCode() == 200) {
String jsonResponse = response.getBody(); acc.Type='Customer';
}
JSONParser parser = JSON.createParser(response.getBody());
HttpResponse res = http.send(request);
//list of header names keys
string[] headerkeys = res.getHeaderKeys();
Map<string, string> headers = new map<string, string>();
//iterate through they keys, and populate your map
for(string s : headerkeys){
headers.put(s,res.getHeader(s));
system.debug('header: ' + s + ' value: ' + res.getHeader(s));
}
}
catch(Exception e){
System.debug('Error-' + e.getMessage());
}
update scope;
}
}
}
global void finish(Database.BatchableContext bc)
{
}
}
First create the below mock class:
Test class:
Actual Batch Class:
Thanks,
Maharajan.C