+ Start a Discussion
Lakshmi SLakshmi S 

Test class for apex schedulable class

Hi All,

I have a schedulable class, it doesn't cover the 100% code coverage, can anyone help me.

Schedulable class
------------------------
public class DailyLeadProcessor implements Schedulable {
    public void execute(SchedulableContext sc){
        List<Lead> updaterec = new List<Lead>();
        List<Lead> leadrec = [select id,lastname,leadsource from Lead where leadsource=null limit 200];
        for(Lead le : leadrec){
            
            updaterec.add(new Lead(id = le.Id,LeadSource='Dreamforce'));
        }
        update updaterec;
    }
    
}

Test Class
--------------
@isTest
private class DailyLeadProcessorTest {
    public static string cron_exp = '0 56 11 * * ?';
    static testMethod void meth(){
        List<Lead> le = new List<Lead>();
        for(integer i=0;i<200;i++){
            Lead l = new Lead();
            l.LastName = 'sfdcuser '+i;
            l.Company = 'Fujistu';
            l.Status = 'Open - Not Contacted';
            le.add(l);
        }
        insert le;
        Test.startTest();
        List<Lead> leadlist2 = new List<Lead>();
        List<Lead> leadlist = [select id from lead where leadsource = null];
        for(Lead lead1 : leadlist){
            leadlist2.add(new Lead(id=lead1.Id,leadsource='Dreamforce'));
        }
        update leadlist2;
        
        DailyLeadProcessor dlp = new DailyLeadProcessor();
        
        
        
        String jobid = System.Schedule('leadtest',cron_exp,dlp);
        
        Test.stopTest();
        
        System.assertEquals(200, leadlist.size());
        
    }

}


Thanks in advance...
Regards,
Narasimha.
Best Answer chosen by Lakshmi S
saikrishna.Gsaikrishna.G
Try this one 

@isTest
public class DailyLeadProcessorTest {

    @isTest Static Void testschedule(){
        Lead l=new Lead(LastName ='Testdata',Company='Apple',Status ='Status ');
        insert l;
        test.startTest();
        system.schedule('testschedule', '0 56 11 * * ?', new DailyLeadProcessor());
       test.stopTest();
    }
    
}