You need to sign in to do that
Don't have an account?
Krutarth 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:
Test Class:
Thanks,
Krutarth Soni.
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.
Plese use the following code.
Thanks,
Arpit vijayvergiya
but still, mail method that is inside for loop is not covered yet.
If you found this answer helpful then please mark it as best answer so it can help others.
Thanks
Akshay
@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();
}
}
Thanks for the replay. but still, mail method inside for loop is not covered.
Thanks
Krutarth Soni.