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
Guru 91Guru 91 

Help me test class with extended class?

Hi,

Can you please help me with test  class
Class:
global class CDPWeeklyDigestBatchScheduler implements Schedulable {

    global void execute(SchedulableContext sc) {
        String query = 'SELECT Id, Deal_Id__c, Deal_Name__c, Deal_Stage__c, Type__c, Region_Type__c, Market__c, Deal_Owner__c, Latest_Change__c, Deal_Lost_Reason__c, Number_Of_Updates__c FROM CDP_Notification__c WHERE LastModifiedDate = LAST_N_DAYS:7 ';
        List<SObject> records = Database.query(query);    
        NotificationHelper.getNotificationHelperInstance().notifyUsersAboutWeeklyDigest(records);
    }








 
Best Answer chosen by Guru 91
Steven NsubugaSteven Nsubuga
Use the right data types for the CDP_Notification__c fields, edit the test as necessary
@isTest
public class CDPWeeklyDigestBatchSchedulerTest{

	@isTest static void test1(){
	 
	Test.startTest();
	CDP_Notification__c notification = new CDP_Notification__c();
	notification.Deal_Id__c = 'Deal Id';
	notification.Deal_Name__c = 'Name';
	notification.Type__c = 'Type';
	notification.Region_Type__c = 'Region';
	notification.Market__c = 'Market';
	notification.Latest_Change__c = 'Change';
	notification.Deal_Lost_Reason__c = 'Reason';
	notification.Number_Of_Updates__c = 5;
	
    insert notification;

    public static String CRON_EXP = '0 0 0 3 9 ? 2022';
       // Schedule the test job
    String jobId = System.schedule('CDPWeeklyDigestBatchScheduler', CRON_EXP, new CDPWeeklyDigestBatchScheduler());
    // Get the information from the CronTrigger API object 
    CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId]; 
    // Verify the job has not run 
    System.assertEquals(0, ct.TimesTriggered); 
      
      Test.stopTest();        
    }
}

 

All Answers

Niraj Kr SinghNiraj Kr Singh
Hi Guru,

Please follow this post, it will be very helpful for you to write test class for your schedulable class:
http://amitsalesforce.blogspot.com/2017/07/how-to-write-test-class-for-scheduler.html

Let me know if you face any issue.
Thanks
Niraj
Steven NsubugaSteven Nsubuga
Use the right data types for the CDP_Notification__c fields, edit the test as necessary
@isTest
public class CDPWeeklyDigestBatchSchedulerTest{

	@isTest static void test1(){
	 
	Test.startTest();
	CDP_Notification__c notification = new CDP_Notification__c();
	notification.Deal_Id__c = 'Deal Id';
	notification.Deal_Name__c = 'Name';
	notification.Type__c = 'Type';
	notification.Region_Type__c = 'Region';
	notification.Market__c = 'Market';
	notification.Latest_Change__c = 'Change';
	notification.Deal_Lost_Reason__c = 'Reason';
	notification.Number_Of_Updates__c = 5;
	
    insert notification;

    public static String CRON_EXP = '0 0 0 3 9 ? 2022';
       // Schedule the test job
    String jobId = System.schedule('CDPWeeklyDigestBatchScheduler', CRON_EXP, new CDPWeeklyDigestBatchScheduler());
    // Get the information from the CronTrigger API object 
    CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId]; 
    // Verify the job has not run 
    System.assertEquals(0, ct.TimesTriggered); 
      
      Test.stopTest();        
    }
}

 
This was selected as the best answer
Guru 91Guru 91
Thanks Steven