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
Pallavi singhPallavi singh 

Need help to get the full 100% coverage for the batch class

Hi,
Need help to complete the test class 
here is the batch class:

@isTest
private class CreateLeadDLCRecordsBatchTest {
    @isTest
    static void testBatchExecution() {
        // Create test data
        List<Lead> leads = new List<Lead>();
        for (Integer i = 0; i < 10; i++) {
            Lead lead = new Lead();
            lead.FirstName = 'Test';
            lead.LastName = 'Lead ' + i;
            lead.Status = 'Converted';
           // lead.Retention_Date_Reached__c = false;
            leads.add(lead);
        }
        insert leads;

        List<Lead_Status_History__c> leadStatusHistories = new List<Lead_Status_History__c>();
        for (Lead lead : leads) {
            Lead_Status_History__c history = new Lead_Status_History__c();
            history.Lead__c = lead.Id;
            // Set other required fields
            leadStatusHistories.add(history);
        }
        insert leadStatusHistories;

        // Start the batch
        Test.startTest();
        CreateLeadDLCRecordsBatch batch = new CreateLeadDLCRecordsBatch();
        Database.executeBatch(batch);
        Test.stopTest();

        // Verify the results
        Integer expectedDLCRecords = 10;
        Integer actualDLCRecords = [SELECT COUNT() FROM DLC_Control__c];
        System.assertEquals(expectedDLCRecords, actualDLCRecords, 'Incorrect number of DLC_Control__c records created');
       
        Set<Id> expectedLeadStatusHistoryIds = new Set<Id>();
        for (Lead_Status_History__c history : leadStatusHistories) {
            expectedLeadStatusHistoryIds.add(history.Id);
        }
        Set<Id> actualLeadStatusHistoryIds = new Set<Id>();
        for (DLC_Control__c dlc : [SELECT Record_Id__c FROM DLC_Control__c]) {
            actualLeadStatusHistoryIds.add(dlc.Record_Id__c);
        }
       // System.assertEquals(expectedLeadStatusHistoryIds, actualLeadStatusHistoryIds, 'Incorrect Lead_Status_History__c records processed');
    }
 
}
 

and this is my test class which covers 85% but i need 100%.

@isTest
private class CreateLeadDLCRecordsBatchTest {
    @isTest
    static void testBatchExecution() {
        // Create test data
        List<Lead> leads = new List<Lead>();
        for (Integer i = 0; i < 10; i++) {
            Lead lead = new Lead();
            lead.FirstName = 'Test';
            lead.LastName = 'Lead ' + i;
            lead.Status = 'Converted';
           // lead.Retention_Date_Reached__c = false;
            leads.add(lead);
        }
        insert leads;

        List<Lead_Status_History__c> leadStatusHistories = new List<Lead_Status_History__c>();
        for (Lead lead : leads) {
            Lead_Status_History__c history = new Lead_Status_History__c();
            history.Lead__c = lead.Id;
            // Set other required fields
            leadStatusHistories.add(history);
        }
        insert leadStatusHistories;

        // Start the batch
        Test.startTest();
        CreateLeadDLCRecordsBatch batch = new CreateLeadDLCRecordsBatch();
        Database.executeBatch(batch);
        Test.stopTest();

        // Verify the results
        Integer expectedDLCRecords = 10;
        Integer actualDLCRecords = [SELECT COUNT() FROM DLC_Control__c];
        System.assertEquals(expectedDLCRecords, actualDLCRecords, 'Incorrect number of DLC_Control__c records created');
       
        Set<Id> expectedLeadStatusHistoryIds = new Set<Id>();
        for (Lead_Status_History__c history : leadStatusHistories) {
            expectedLeadStatusHistoryIds.add(history.Id);
        }
        Set<Id> actualLeadStatusHistoryIds = new Set<Id>();
        for (DLC_Control__c dlc : [SELECT Record_Id__c FROM DLC_Control__c]) {
            actualLeadStatusHistoryIds.add(dlc.Record_Id__c);
        }
       // System.assertEquals(expectedLeadStatusHistoryIds, actualLeadStatusHistoryIds, 'Incorrect Lead_Status_History__c records processed');
    }
 
}

Thank you in advance
Arun Kumar 1141Arun Kumar 1141
Hi Pallavi,

To achieve 100% test coverage for your batch class, you need to cover all possible execution paths and scenarios. Here's an example of how you can modify your test class to achieve full coverage:
 
@isTest
private class CreateLeadDLCRecordsBatchTest {
    @isTest
    static void testBatchExecution() {
        // Create test data
        List<Lead> leads = new List<Lead>();
        for (Integer i = 0; i < 10; i++) {
            Lead lead = new Lead();
            lead.FirstName = 'Test';
            lead.LastName = 'Lead ' + i;
            lead.Status = 'Converted';
            leads.add(lead);
        }
        insert leads;

        List<Lead_Status_History__c> leadStatusHistories = new List<Lead_Status_History__c>();
        for (Lead lead : leads) {
            Lead_Status_History__c history = new Lead_Status_History__c();
            history.Lead__c = lead.Id;
            // Set other required fields
            leadStatusHistories.add(history);
        }
        insert leadStatusHistories;

        // Start the batch
        Test.startTest();
        // Execute the batch with a smaller batch size (e.g., 1) for better control over coverage
        CreateLeadDLCRecordsBatch batch = new CreateLeadDLCRecordsBatch();
        batch.queryLimit = 1; // Add a variable to control the batch size
        Database.executeBatch(batch);
        Test.stopTest();

        // Verify the results
        Integer expectedDLCRecords = 10;
        Integer actualDLCRecords = [SELECT COUNT() FROM DLC_Control__c];
        System.assertEquals(expectedDLCRecords, actualDLCRecords, 'Incorrect number of DLC_Control__c records created');

        Set<Id> expectedLeadStatusHistoryIds = new Set<Id>();
        for (Lead_Status_History__c history : leadStatusHistories) {
            expectedLeadStatusHistoryIds.add(history.Id);
        }
        Set<Id> actualLeadStatusHistoryIds = new Set<Id>();
        for (DLC_Control__c dlc : [SELECT Record_Id__c FROM DLC_Control__c]) {
            actualLeadStatusHistoryIds.add(dlc.Record_Id__c);
        }
        System.assertEquals(expectedLeadStatusHistoryIds, actualLeadStatusHistoryIds, 'Incorrect Lead_Status_History__c records processed');

        // Test additional scenarios
        // Scenario 1: Test if the batch handles no leads or lead status histories
        delete leads;
        delete leadStatusHistories;
        Test.startTest();
        batch = new CreateLeadDLCRecordsBatch();
        Database.executeBatch(batch);
        Test.stopTest();
        actualDLCRecords = [SELECT COUNT() FROM DLC_Control__c];
        System.assertEquals(0, actualDLCRecords, 'DLC_Control__c records should not be created when there are no leads');

        // Scenario 2: Test if the batch handles an exception
        // Modify the Lead_Status_History__c records to cause an exception during processing
        leadStatusHistories[0].Lead__c = null; // Set Lead__c to null to cause an exception
        update leadStatusHistories;
        Test.startTest();
        batch = new CreateLeadDLCRecordsBatch();
        Database.executeBatch(batch);
        Test.stopTest();
        // Verify that the exception was handled gracefully (no assertion needed)
    }
}

Hope this will be Helpful.
Thanks!