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 

Write a Test class for scheduled apex class Mon Tue wed thu 7am

Hi Gurus,

Plz help in Writing a Test class for below scheduled apex class Mon Tue wed thu 7am
 
global class CopyERTBatchDaily Implements Schedulable
    {
        global void execute(SchedulableContext sc)
        {
            setERTBatch();
        }

        public void setERTBatch()
        {
            //List<Executive_RT> exeList = new List<Executive_RT>();
            List<Executive_RT> exeList = [select Case__c, Level_1__c, Level_2__c,Level_3__c  FROM Executive_RT];
             // process each batch of records
            List<Case_Type__c> listCTD = new List<Case_Type__c>();
        
        for(Executive_RT 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);
        }
        try {
        	
            insert listCTD;
        
        } catch(Exception e) {
            System.debug(e);
        }
            
        }
    }

Regards
Fiona​​​​​​​
Best Answer chosen by fiona gentry
Maharajan CMaharajan C
HI Fiona,

Please try the below code:

Change the test data as you need.
 
@isTest
private class CopyERTBatchDailyTest
{

    static testmethod void schedulerTest() 
    {
		
        String CRON_EXP = '0 0 10 ? * MON-THU';
		
		// Create your test data to create case record 
		// Add if there is any other fields are to insert case
		case caserec = new caserec(Origin='phone', Status = 'New');
		insert caserec;
        
        // Create your test data to create Executive_RT record 
		// Add if there is any other fields are to insert Executive_RT
        Executive_RT exe = new Executive_RT();
        exe.name= 'test';
		exe.Case__c = caserec.Id;
		exe.Level_1__c = 'Test Level'; // I have assumed this field as text 
		exe.Level_2__c = 'Test Level'; // I have assumed this field as text
		exe.Level_3__c = 'Test Level'; // I have assumed this field as text
        insert exe;
        
        Test.startTest();

            String jobId = System.schedule('ScheduleApexClassTest',  CRON_EXP, new CopyERTBatchDaily());
            CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
            //System.assertEquals(CRON_EXP, ct.CronExpression);
			// Add your assert statements to validate result
        Test.stopTest();
        // Add assert here to validate result
    }
}

Thanks,
Maharajan.C