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
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();
    }
}