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
fiona gentryfiona gentry 

How to make 64% to 100% code coverage for Test class for Batchable to insert records, iterate over the results, and persist information about any failures to the database

Dear Gurus,

How to make 64%  to 100% code coverage for Test class for Batchable to insert records, iterate over the results, and persist information about any failures to the database

Here is class
global class ertnoonbatch3pm implements Database.Batchable<sObject> {
         global Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute
        
        String query = 'select Case__c, Level_1__c, Level_2__c,Level_3__c  FROM ERT_Case_Type__c';
        System.debug('ERT Case No is =====>' +query);
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<ERT_Case_Type__c> exeList) {
        
        // process each batch of records
        List<Case_Type__c> listCTD = new List<Case_Type__c>();
        System.debug('ERT Case No is =====>' +exeList);
        for(ERT_Case_Type__c exe : exeList)
        {        
            listCTD.add(new Case_Type__c(Case__c=exe.Case__c,Level_1__c=exe.Level_1__c,Level_2__c=exe.Level_2__c,Level_3__c=exe.Level_3__c));
            // System.debug('ERT Case No is =====>' +Case__c);
        }

        System.debug('ERT Case No is =====>' +listCTD);
        insert listCTD;
        
        
        Database.SaveResult[] srList = Database.insert(listCTD, false);
        
        // Iterate through each returned result
        for (Database.SaveResult sr : srList) {
            if (sr.isSuccess()) {
                // Operation was successful, so get the ID of the record that was processed
                System.debug('Successfully inserted Case_Type__c: ' + sr.getId());
            }
            else {
                // Operation failed, so get all errors                
                for(Database.Error err : sr.getErrors()) {
                    System.debug('The following error has occurred.');                    
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('Case_Type__c fields that affected this error: ' + err.getFields());
                }
            }
        }            
        
    }   
    
    global void finish(Database.BatchableContext BC) {
        // execute any post-processing operations
    }      

            }

here is test class
@isTest
public class testertnoonbatch3pm{
    static testMethod void testMethod1() 
    {
        List<ERT_Case_Type__c> lstExecutive_RT= new List<ERT_Case_Type__c>();
            Case c = new Case();
        c.Contact_First_Name__c = 'first name';
        c.Contact_Last_Name__c = 'last name';
        c.Address_Line_1__c = 'first name';
        c.Address_Line_2__c = 'first name';
        c.Address_City__c = 'first name';
        c.Address_State__c = 'WA';
        c.Address_Zip__c = '12345';
        c.Home_Phone__c = '123-456-7890';
        c.E_Mail__c = 'abc@abc.com';
        c.hasdevice__c = true;
        c.IMEI__c = 1234567890123.0;
        c.InterestedinTFB__c = true;
        insert c;
        for(Integer i=0 ;i <200;i++)
        {
            ERT_Case_Type__c rt = new ERT_Case_Type__c();
            //5001Q000016wP1BQAU
            rt.Case__c = c.Id;
            rt.Level_1__c ='Customer Service';
            //rt.Level_1__c ='Customer Service'+i;
            rt.Level_2__c ='Customer Care  -  T-Mobile';
            rt.Level_3__c ='Upholding policy - account services';
            system.debug('inserted value'+rt);
           lstExecutive_RT.add(rt); 
            
        
        }
        
        insert lstExecutive_RT;
        
        Test.startTest();

            ertnoonbatch3pm objbatch3pm = new ertnoonbatch3pm();
            
            DataBase.executeBatch(objbatch3pm); 
            
        Test.stopTest();
    }

}

How to make this code100 % code covered ,here are code lines not coveredUser-added image
Best Answer chosen by fiona gentry
RituSharmaRituSharma
it seems that listCTD  is empty. Check the batch query and see if it's fetching some records.

All Answers

RituSharmaRituSharma
it seems that listCTD  is empty. Check the batch query and see if it's fetching some records.
This was selected as the best answer
AbhishekAbhishek (Salesforce Developers) 
Increase code coverage

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

75 %

https://salesforce.stackexchange.com/questions/24165/why-is-75-code-coverage-required-in-salesforce/24167#24167

Go through the suggestions as mentioned in the above discussion, so that it can help you further.