You need to sign in to do that
Don't have an account?
sksfdc221
Test Class for REST API Class
I have an apex class which fetches and upserts data from external source to Salesforce.
Below is the batch class:
Below is my Mock test class:
Below is my test class:
Though I am not seeing any errors in my code, my batch apex class is covered only 8%. Complete execute method is left uncovered.
Can anyone please suggest the changes to my above test class so that I can get this done.
Thank you in advance!
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:
global class MockHttpResponseGenerator implements HttpCalloutMock { // Implement this interface method global HTTPResponse respond(HTTPRequest req) { String jsonbody = '{ ' + '"id": "11111"' + '}' ; HttpResponse objHttpResponse = new HttpResponse(); objHttpResponse.setBody(jsonbody); objHttpResponse.setHeader('Content-Type', 'application/json'); objHttpResponse.setStatusCode(200); return objHttpResponse; } }
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(); } }
Though I am not seeing any errors in my code, my batch apex class is covered only 8%. Complete execute method is left uncovered.
Can anyone please suggest the changes to my above test class so that I can get this done.
Thank you in advance!
Please go through the below code it may helpful for you.
@isTest
private class PolicyUpdateController_Test {
static testMethod void testPostCallout() {
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
Test.startTest();
PolicyCalloutBatchClass obj = new PolicyCalloutBatchClass ();
Database.executeBatch(obj, 50);
Test.stopTest();
}
}
In the testPostCallout method, the PolicyCalloutBatchClass batch class is instantiated, invoked by calling Database.executeBatch and passing it the instance of the batch class.
I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com
Hi Sachin,
I have updated my test class based on your suggestion and even now, I could see that my batch class is covered only 8%.
Am I missing anything or could you please suggest any changes here?