function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Atul Shendge 2Atul Shendge 2 

Test class coverage need to be reached at 90%

Hi Team,

There is a batch class where a code coverage is 12%. need at 90% to push it to Production

Below is the batch class and Test class.

Batch class:
global class THX_Batch_UpdateType_Channel_Call implements Database.Batchable<sObject>, Schedulable{

    //Query to all calls modified today and yesterday, in status submitted, and not executed in this batch
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator('SELECT Id, Date__c, Status__c, Channel__c, CallType__c, THX_Updated_Type_Channel__c' +
                                        ' FROM Call__c' +
                                        ' WHERE lastModifieddate = LAST_N_DAYS:1 and (NOT CreatedBy.Profile.Name LIKE \'%Admin%\') AND Status__c LIKE \'Submitted\' AND THX_Updated_Type_Channel__c = false AND ParentCall__c = null');
    }

    global void execute(Database.BatchableContext BC, List<Call__c> scope){
        list<Call__c> listAttendees = [SELECT id, ParentCall__c FROM Call__c WHERE ParentCall__c in :scope];
        list<CallPresentation__c> listCallPresent = [SELECT id, Call__c FROM CallPresentation__c WHERE Call__c in :scope];
        
        if(listCallPresent.size() > 0 || listAttendees.size() > 0){
            set<id> setIdCallUpdateByPresentation = new set<id>();
            for(CallPresentation__c callPres: listCallPresent){
                setIdCallUpdateByPresentation.add(callPres.Call__c);
            }
            
            set<id> setIdCallUpdateByAttendees = new set<id>();
            set<Id> setCallWithAttendees = new set<Id>();
            map<id, list<Call__c>> mapAttendeeToUpdate = new map<id, list<Call__c>>();
            
            for(Call__c attendee: listAttendees){
                setIdCallUpdateByAttendees.add(attendee.ParentCall__c);
                if(mapAttendeeToUpdate.get(attendee.ParentCall__c) == null){
                    list<Call__c> listAttend = new list<Call__c>{attendee};
                    mapAttendeeToUpdate.put(attendee.ParentCall__c, listAttend);
                }else{
                    list<Call__c> listAttend = mapAttendeeToUpdate.get(attendee.ParentCall__c);
                    listAttend.add(attendee);
                    mapAttendeeToUpdate.put(attendee.ParentCall__c, listAttend);
                }
            }
            
            list<Call__c> listCallUpdate = new list<Call__c>();
            if(setIdCallUpdateByPresentation.size() > 0 || setIdCallUpdateByAttendees.size() > 0){
                for(Call__c call: scope){
                    boolean updateCall = false;
                    if(setIdCallUpdateByPresentation.contains(call.id)){
                        call.Channel__c = 'EC';
                        updateCall = true;
                        if(mapAttendeeToUpdate.get(call.id) != null){
                            for(Call__c attend: mapAttendeeToUpdate.get(call.id)){
                                attend.Channel__c = 'EC';
                            }
                        }
                    }
                    if(setIdCallUpdateByAttendees.contains(call.id) && mapAttendeeToUpdate.get(call.id).size() > 1){
                        call.CallType__c = 'Group Call';
                        updateCall = true;
                        if(mapAttendeeToUpdate.get(call.id) != null){
                            for(Call__c attend: mapAttendeeToUpdate.get(call.id)){
                                attend.CallType__c = 'Group Call';
                            }
                        }
                    }
                    if(updateCall){
                        call.THX_Updated_Type_Channel__c = true;
                        listCallUpdate.add(call);
                        if(mapAttendeeToUpdate.get(call.id) != null){
                            for(Call__c attend: mapAttendeeToUpdate.get(call.id)){
                                attend.THX_Updated_Type_Channel__c = true;
                                listCallUpdate.add(attend);
                            }
                        }
                    }
                }
                update listCallUpdate;                
            }
            
        }
        
    }

   global void finish(Database.BatchableContext BC){
   }
    
  global void execute(SchedulableContext SC) {
        THX_Batch_UpdateType_Channel_Call batch = new THX_Batch_UpdateType_Channel_Call();
        Id batchId = Database.executeBatch(batch, 200);
    }
    
    
}

Test class:
@isTest(SeeAllData=false)
public class THX_Batch_UpdateType_Channel_Call_Test {
    
    static void setup() {
        Profile p = [SELECT Id FROM Profile WHERE Name='THX Rep'];
        String nameUser = 'standarduser' + DateTime.now().getTime() + '@testorg.com';
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
                            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
                            LocaleSidKey='en_US', ProfileId = p.Id,Phone='123',
                            TimeZoneSidKey='America/Los_Angeles',MobilePhone='123',
                              UserName=nameUser, firstname='Firstname', Street='123');
        
        System.runAs(u) {
            Account ac = THX_TestDataFactory.createAccount('MP');
            insert ac;
            
            Product__c prod = THX_TestDataFactory.createProduct('NameProd');
            insert prod;
        
            Presentation__c presentation = THX_TestDataFactory.createPresent('NamePresentation');
            insert presentation;
            
            Call__c call = THX_TestDataFactory.createCall(ac.id, system.now());
            call.Status__c = 'Submitted';
            insert call;
            
            CallDetail__c callDet = THX_TestDataFactory.createCallDetail(call.id, prod.id);
            insert callDet;
            
            Call__c callAttendee1 = THX_TestDataFactory.createCall(ac.id, call.CallDateTime__c);
            callAttendee1.ParentCall__c = call.id;
            Call__c callAttendee2 = THX_TestDataFactory.createCall(ac.id, call.CallDateTime__c);
            callAttendee2.ParentCall__c = call.id;
            list<Call__c> listAtt = new list<Call__c>{callAttendee1, callAttendee2};
            insert listAtt;
            
            CallPresentation__c callPres = THX_TestDataFactory.createCallPresent(call.id, presentation.id);
            insert callPres;
          }
        
    }
    
    static testmethod void test() {        
            THX_Batch_UpdateType_Channel_Call batchUpdateCalls = new THX_Batch_UpdateType_Channel_Call();
            Id batchId = Database.executeBatch(batchUpdateCalls);
    }
    
    static testMethod void updateCallsScheduled(){
        System.Test.startTest();
            THX_Batch_UpdateType_Channel_Call batch = new THX_Batch_UpdateType_Channel_Call();
            Datetime dt = Datetime.now();
            dt = dt.addMinutes(1);
            String timeForScheduler = dt.format('s m H d M \'?\' yyyy'); 
            Id schedId = System.Schedule('Test Batch update calls', timeForScheduler, batch);
        System.Test.stopTest();
    }
    
}
AbhinavAbhinav (Salesforce Developers) 
Check this:

https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

Thanks!