I have an apex class which fetches and upserts data from external source to Salesforce.
Below is the batch class:
global class PolicyCalloutBatchClass implements Database.Batchable<Integer>,Database.AllowsCallouts, Database.Stateful {
global Iterable<Integer> start(Database.BatchableContext context) {
Integer[] values = new Integer[0];
while(values.size() < 2999 || Test.isRunningTest()) values.add(values.size());
return values;
}
global void execute(Database.BatchableContext context, Integer[] values) {
HttpRequest policyreq = new HttpRequest();
policyreq.setMethod('GET');
policyreq.setTimeout(120000);
policyreq.setEndpoint('<endpoint>');
policyreq.setHeader('Authorization', 'Bearer ' + <token>);
Http policyhttp = new Http();
HTTPResponse policyres = policyhttp.send(policyreq);
String policyresponse = policyres.getBody();
JsonParser objJsonParser = (JsonParser) JSON.deserialize(policyresponse, JsonParser.class);
JsonParser.cls_value clsValue = objJsonParser.value;
Map<String, JsonParser.cls_data> clsDataMap = new Map<String, JsonParser.cls_data>();
for(JsonParser.cls_data objClsData: clsValue.data){
clsDataMap.put(objClsData.id, objClsData);
}
list<Policy__c> updatelist = new list<Policy__c>();
for (String eachIdFromMap : clsDataMap.keySet()){
Policy__c policy = new Policy__c(
unique_id__c = clsDataMap.get(eachIdFromMap).id,
agent_id__c = clsDataMap.get(eachIdFromMap).agentId);
updatelist.add(policy);
}
try{
upsert updatelist unique_id__c;
}
catch(DmlException e){
system.debug('This class didnt compile');
}
}
global void finish(Database.BatchableContext context) {
}
}
Below is my Mock test class:
@isTest
global class MockHttpResponseGenerator implements HttpCalloutMock {
// Implement this interface method
global HTTPResponse respond(HTTPRequest req) {
HttpRequest policyreq = new HttpRequest();
policyreq.setMethod('GET');
policyreq.setTimeout(120000);
policyreq.setEndpoint('<endpoint>');
policyreq.setHeader('Authorization', 'Bearer ' + <token>);
Http policyhttp = new Http();
HTTPResponse policyres = policyhttp.send(policyreq);
String policyresponse = policyres.getBody();
return policyres;
}
}
Below is my test class:
@isTest
private class PolicyUpdateController_Test {
static testMethod void testPostCallout() {
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
Test.startTest();
Database.executeBatch(new PolicyCalloutBatchClass(), 50);
Test.stopTest();
}
}
Now, when I run the above test class, either the test class runs forever without any output or I could see the error as
System.LimitException: Too many callouts: 101
Can anyone please let me know if there is any way to get this done?
Thanks!
Test.isRunningTest
in code is not best practice and can be avoided by injecting the max array size via a constructor argument.