You need to sign in to do that
Don't have an account?
dhanraj r
How to Cover the test class for batch apex callout
global class CouponBatch implements Database.Batchable<sObject>, Database.AllowsCallouts{
global Database.QueryLocator start(Database.BatchableContext bc){
String soqlQuery = 'SELECT id, CouponCode__c, Redeemed_Code__c, Type From order';
return Database.getQueryLocator(soqlQuery);
}
global void execute(Database.BatchableContext bc, List<Order> orderDetails){
try {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:couponcode_101/couponcode.json');
req.setMethod('GET');
Http http = new Http();
HttpResponse res = http.send(req);
if (res.getStatusCode() == 200) {
Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
List<Object> resultslist = (List<Object>)results.get('discount_codes');
for(order datadetails: orderDetails) {
for(Object mapa:resultslist ){
Map<String,Object> tempMap = (Map<String,Object>)mapa;
if(datadetails.CouponCode__c==tempMap.get('code')){
datadetails.Redeemed_Code__c=true;
}
}
}
update orderDetails;
}
}
catch(Exception e){
system.debug('Message: '+e.getMessage());
system.debug('Line: '+e.getLineNumber());
}
}
global void finish(Database.BatchableContext bc){
}
}
global Database.QueryLocator start(Database.BatchableContext bc){
String soqlQuery = 'SELECT id, CouponCode__c, Redeemed_Code__c, Type From order';
return Database.getQueryLocator(soqlQuery);
}
global void execute(Database.BatchableContext bc, List<Order> orderDetails){
try {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:couponcode_101/couponcode.json');
req.setMethod('GET');
Http http = new Http();
HttpResponse res = http.send(req);
if (res.getStatusCode() == 200) {
Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
List<Object> resultslist = (List<Object>)results.get('discount_codes');
for(order datadetails: orderDetails) {
for(Object mapa:resultslist ){
Map<String,Object> tempMap = (Map<String,Object>)mapa;
if(datadetails.CouponCode__c==tempMap.get('code')){
datadetails.Redeemed_Code__c=true;
}
}
}
update orderDetails;
}
}
catch(Exception e){
system.debug('Message: '+e.getMessage());
system.debug('Line: '+e.getLineNumber());
}
}
global void finish(Database.BatchableContext bc){
}
}
below is your test class with 95% code coverage.keep one thing in mind that if you need to update your CouponCode__c then must update
with below line code.
for example if you update your CouponCode__c in test class with 11111 then it must be update [{"code":"1111"}]
response.setBody('{"discount_codes":[{"code":"055"}]}');
don't forget to mark it as best answer.
Thank you
All Answers
The code provided in question does not highlight the uncovered lines. The below articles give a good insight into how coverage can be improved
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines
Examples of test class for batch apex callout:
https://salesforce.stackexchange.com/questions/20546/test-class-for-batch-apex-with-webservice-callout
https://salesforce.stackexchange.com/questions/19444/batch-apex-with-webservice-callout?rq=1
https://salesforce.stackexchange.com/questions/130639/test-class-for-batch-that-sends-callout?rq=1
Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
Hi Swetha,
The below section is not covered
if (res.getStatusCode() == 200) {
Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
List<Object> resultslist = (List<Object>)results.get('discount_codes');
for(order datadetails: orderDetails) {
for(Object mapa:resultslist ){
Map<String,Object> tempMap = (Map<String,Object>)mapa;
if(datadetails.CouponCode__c==tempMap.get('code')){
datadetails.Redeemed_Code__c=true;
}
}
}
update orderDetails;
}
below is your test class with 95% code coverage.keep one thing in mind that if you need to update your CouponCode__c then must update
with below line code.
for example if you update your CouponCode__c in test class with 11111 then it must be update [{"code":"1111"}]
response.setBody('{"discount_codes":[{"code":"055"}]}');
don't forget to mark it as best answer.
Thank you
Hi Veer Soni,
Thanks for your help. Now code coverage is 90%.