You need to sign in to do that
Don't have an account?
Hi everyone, I'm new to Salesforce development, and facing in issue with Batch class I'm trying to create test class but I end up with only 18% coverage
Here is the code for the Batch class:
And currently, I only figured out this testing class:
And here the uncovered part of the Batch class:

I will appreciate any kind of help.
Thank you
global class SmsCounterBatch implements Database.Batchable<sObject> { global Database.QueryLocator start(Database.BatchableContext BC) { return Database.getQueryLocator([ SELECT Id, Total_SMS_Delivered__c, Total_SMS_Failed__c, Total_SMS_Sent__c, ( SELECT Id, SMS_Delivery_Status__c FROM ActivityHistories WHERE (ActivityType = 'SMS') AND (Status = 'Completed') ) FROM Campaign WHERE (IsActive = TRUE) AND (EndDate >= TODAY) ORDER BY LastModifiedDate DESC ]); } global void execute(Database.BatchableContext BC, List<Campaign> scope) { List<Campaign> campaignToUpdate = new List<Campaign>(); for(Campaign campaign : scope) { if(!campaign.ActivityHistories.isEmpty()) { campaign.Total_SMS_Delivered__c = 0; campaign.Total_SMS_Failed__c = 0; campaign.Total_SMS_Sent__c = 0; for(ActivityHistory record : campaign.ActivityHistories) { if(record.SMS_Delivery_Status__c == 'Delivered') campaign.Total_SMS_Delivered__c++; else if(record.SMS_Delivery_Status__c == 'OK') campaign.Total_SMS_Sent__c++; else campaign.Total_SMS_Failed__c++; } campaignToUpdate.add(campaign); } } if(!campaignToUpdate.isEmpty()) update campaignToUpdate; } global void finish(Database.BatchableContext BC) { } }
And currently, I only figured out this testing class:
@isTest(SeeAllData=true) public class SmsCounterBatchTest { @IsTest public static void couterBatchTestMethod() { Campaign cam1 = new Campaign (); cam1.Name = 'Test Campaign 1'; cam1.IsActive = True; cam1.Total_SMS_Delivered__c = 0; cam1.Total_SMS_Sent__c = 0; cam1.Total_SMS_Failed__c = 0; Insert cam1; Campaign cam2 = new Campaign(); cam2.Name = 'Test Campaign 2'; cam2.IsActive = True; cam2.Total_SMS_Delivered__c = 0; cam2.Total_SMS_Sent__c = 0; cam2.Total_SMS_Failed__c = 0; Insert cam2; Contact c = new Contact(); c.LastName = 'Test Account'; insert c; SMS__c sms = new SMS__C(); sms.Campaign__c = cam1.Id; sms.Delivery_Status__c = 'Delivered'; sms.Message__c = 'Test Message'; sms.Recipient__c = c.Id; sms.Transaction_ID__c = '1b9f8ac8eb75494e80f1474aeb61d93d'; insert sms; List<Campaign> campaignList = [SELECT Id, Total_SMS_Delivered__c, Total_SMS_Failed__c, Total_SMS_Sent__c, ( SELECT Id, SMS_Delivery_Status__c FROM ActivityHistories WHERE (ActivityType = 'SMS') AND (Status = 'Completed') ) FROM Campaign WHERE (IsActive = TRUE)]; if(!campaignList.isEmpty()) { cam1.Total_SMS_Delivered__c = 0; cam1.Total_SMS_Sent__c = 0; cam1.Total_SMS_Failed__c = 0; } update cam1; Test.StartTest(); SmsCounterBatch counterBatch = new SmsCounterBatch(); Database.executeBatch(counterBatch); Test.StopTest(); System.AssertEquals(database.countquery('SELECT COUNT()'+' FROM SMS__c'), 21); } }
And here the uncovered part of the Batch class:
I will appreciate any kind of help.
Thank you
Let us know if this will help you
All Answers
Let us know if this will help you
Thank you for the reply. I did and helped me but only for 50% coverage, and here the uncovered code :
Thank you for the answer again. I managed to get the required coverage successfully.