+ Start a Discussion
Muhammad Jawwad 16Muhammad Jawwad 16 

Schedule apex test class

How do I test schedule apex? please help
global class MonthlyPipelineReportScheduled implements Schedulable 
{
    global void execute(SchedulableContext sc) 
    {
    

        LoanofficerBatch lob = new LoanofficerBatch(); 
        database.executebatch(lob);
        
        RealtorBatch rb = new RealtorBatch(); 
        database.executebatch(rb);
        
        BuilderBatch bb = new BuilderBatch(); 
        database.executebatch(bb);
    }
}

 
Best Answer chosen by Muhammad Jawwad 16
Raj VakatiRaj Vakati
Use this code and set some sort of the data for all the batchs 

 
@isTest
private class MonthlyPipelineReportScheduledTest {
    
    public static String CRON_EXP = '0 0 0 15 3 ? 2022';
    static testmethod void testScheduledJob() {
        // Create some set of data for all the batch jobs 
        List<Opportunity> opptys = new List<Opportunity>();
        Date closeDate = Date.today().addDays(-7);
        for (Integer i=0; i<10; i++) {
            Opportunity o = new Opportunity(
                Name = 'Opportunity ' + i,
                CloseDate = closeDate,
                StageName = 'Prospecting'
            );
            opptys.add(o);
        }
        insert opptys;
        

        Test.startTest();
        String jobId = System.schedule('MonthlyPipelineReportScheduledJobs',
            CRON_EXP, 
            new MonthlyPipelineReportScheduled ());         
        Test.stopTest();
    }
}