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
Krutarth SoniKrutarth Soni 

How to cover Schedule Class in Test Class

Please help me to cover Schedule Class in Test Class. Help me to achieve 100% code coverage.

Schedule Class:

global class ScheduleContactDelete implements Schedulable {
    
  
    global void execute (SchedulableContext ctx)
    {
        
        // Delete Contact
        List<Contact> con = [SELECT LastName FROM Contact WHERE Id IN (SELECT Contact__c FROM GDPR_Permission__c 
                                                                       WHERE Retention_Expiration_Date__c  = :Date.Today() AND Communication_Rule_Active__c = TRUE)];
        delete con;
        system.debug('Deleted Contacts :'+ con);
        
        // Delete Contact Mail Send
        
        List<Contact> con1 = [SELECT Id, Email  FROM Contact WHERE Id IN (SELECT Contact__c FROM GDPR_Permission__c 
                                                                          WHERE Retention_Expiration_Date__c  = :Date.Today())]; 
        
     //   List<Id> lstid = new List<Id>();
        for(Contact c : con1)
        {
         //   lstid.add(c.id);
            
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                   
            mail.setToAddresses(new String[] {c.Email}); 
            mail.setSubject('Consent Data Expired' ); 
            mail.setUseSignature(false);
            mail.setHtmlBody('Dear '+c.FirstName+', <br>Your Consent Data has been deleted permanently from our System.<br>Reason: Consent for Communication has been expired. <br><br>Thank you! <br>Confident Governance Team'); 
            // Send the email
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
            
            
        }
        
        // Consent Mail
        List<Contact> con2 = [SELECT LastName FROM Contact WHERE Id IN (SELECT Contact__c FROM GDPR_Permission__c 
                                                                        WHERE Retention_Expiration_Date__c  = :Date.Today() AND Communication_Rule_Active__c = FALSE)];
      //  List<Id> Con2ID = new List<Id>();
        for(Contact c1 : con2)
        {
       //     Con2ID.add(c1.id);
            
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                   
            mail.setToAddresses(new String[] {c1.Email}); 
            mail.setSubject('Keep Consent Details' ); 
            mail.setUseSignature(false);
            mail.setHtmlBody('Dear '+c1.FirstName+', <br><br>Communication has been stopped but we are keeping your consent details because your details are connected with our product.<br>Reason: For Legal purpose. <br>Notice: Please Contact us for future communication. <br><br>Thank you! <br>Confident Governance Team'); 
            // Send the email
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
            
            
        }
    }
    
    
}

Test Class:
 
@isTest
private class scheduleContactDeleteTest {
    
    public static string cronExp = '0 0 0 28 2 ? 2022';
    
    static testmethod void deleteExecute(){
        
        ScheduleContactDelete ss =new ScheduleContactDelete();
        
        test.startTest();
        String jobId = System.schedule('Student Information', cronExp , new ScheduleContactDelete());        
       
        contact c= new contact();
        c.Email='test@test.com';
        c.LastName='test';
        insert c;
        
        test.stopTest();
    }
}

Thanks,
Krutarth Soni.
 
arpit vijayvergiyaarpit vijayvergiya
Hi Krutarth,

Plese use the following code.
 
@isTest
private class scheduleContactDeleteTest {
    String hour = String.valueOf(Datetime.now().hour());
    String min = String.valueOf(Datetime.now().minute() + 1);
    String ss = String.valueOf(Datetime.now().second());
    String cronExp = ss + ' ' + min + ' ' + hour + ' * * ?';
    
    static testmethod void deleteExecute(){
        
        ScheduleContactDelete ss =new ScheduleContactDelete();
        
        test.startTest();
        String jobId = System.schedule('Student Information', cronExp , new ScheduleContactDelete());        
       
        contact c= new contact();
        c.Email='test@test.com';
        c.LastName='test';
        insert c;
        
        test.stopTest();
    }
}

Thanks,
Arpit vijayvergiya
 
Krutarth SoniKrutarth Soni
Thanks for the reply!

but still, mail method that is inside for loop is not covered yet.
arpit vijayvergiyaarpit vijayvergiya
You need to insert records before this line. System.schedule('Student Information', cronExp , new ScheduleContactDelete()); Thanks, Arpit vijayvergiya Salesforce Developer
Krutarth SoniKrutarth Soni
I already tried but still, it's not covered yet.
arpit vijayvergiyaarpit vijayvergiya
You need to insert records , that satisfy your soql query. Loop will executed only if there records in the list. So insert records as per the soql.
Akshay_DhimanAkshay_Dhiman
Hi Krutarth Soni,
 
@isTest
private class scheduleContactDelete_Test {
    String h = String.valueOf(Datetime.now().hour());
    String m = String.valueOf(Datetime.now().minute() + 1);
    String s = String.valueOf(Datetime.now().second());
    String cronExp = s + ' ' + m + ' ' + h + ' * * ?';
    
    static testmethod void testmethod(){
        
        ScheduleContactDelete ss =new ScheduleContactDelete();
        
        System.test.startTest();
        String jobId = System.schedule('Student Information', cronExp , new ScheduleContactDelete());        
       
        contact conObj= new contact();
        conObj.Email='test@test.com';
        conObj.LastName='test';
        insert conObj;
        GDPR_Permission__c gdprObj  = new GDPR_Permission__c();
        gdprObj.Contact__c = conObj.id;
        gdprObj.Retention_Expiration_Date__c = Date.today();
        // fill all required of custom object GDPR_Permission__c.
        Insert gdprObj;
        
        System.test.stopTest();
    }
}

If you found this answer helpful then please mark it as best answer so it can help others.   
  
  Thanks 
  Akshay
PawanKumarPawanKumar
Please try below.

@isTest
private class scheduleContactDeleteTest {
    String hour = String.valueOf(Datetime.now().hour());
    String min = String.valueOf(Datetime.now().minute() + 1);
    String ss = String.valueOf(Datetime.now().second());
    String cronExp = ss + ' ' + min + ' ' + hour + ' * * ?';
    
    @testSetup static void testDataSetup() {
        contact c= new contact();
        c.Email='test@test.com';
        c.LastName='test';
        insert c;
        
        FROM GDPR_Permission__c dgprPermsion1 = new FROM GDPR_Permission__c();
        dgprPermsion1.Contact__c = c.Id;
        dgprPermsion1.Retention_Expiration_Date__c = Date.Today();
        dgprPermsion1.Communication_Rule_Active__c = TRUE;
        insert dgprPermsion1;
        
        FROM GDPR_Permission__c dgprPermsion2 = new FROM GDPR_Permission__c();
        dgprPermsion2.Contact__c = c.Id;
        dgprPermsion2.Retention_Expiration_Date__c = Date.Today();
        dgprPermsion2.Communication_Rule_Active__c = FALSE;
        insert dgprPermsion2;
        
    }

    
    static testmethod void deleteExecute(){
        ScheduleContactDelete ss =new ScheduleContactDelete();
        test.startTest();
        String jobId = System.schedule('Student Information', cronExp , new ScheduleContactDelete());        
        test.stopTest();
    }
}
 
Krutarth SoniKrutarth Soni
Hello Pawan & Akshay,

Thanks for the replay. but still, mail method inside for loop is not covered.

Thanks
Krutarth Soni.