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
Shruthi MN 19Shruthi MN 19 

Schedule Class code coverage is 0%

I have written a schedule class to update the lead source to web if the lead source is null. I have written test class as well. But code coverage is 0% can anyone help me so that code coverage is 100%

Class 

global class LeadMaintanace implements Schedulable{
    global void execute(SchedulableContext SC) {
        List<Lead> li =[Select Id,Name from Lead where LeadSource = 'null' LIMIT 10];
        List<Lead> lu= new  List<Lead>();
        if(!li.isEmpty()){
            for(Lead ld:li){
                ld.LeadSource = 'Web';
                lu.add(ld);
            }
                }
        update lu;
    }

}

test class

@isTest
public class TestLeadMaintanace {
    @testsetup
    static void setup(){
        List<Lead> li = new List<Lead>();
        for(Integer i;i<10;i++){
            Lead ld = new Lead(Company = 'Proseraa',LastName ='MN',Status = 'Working - Contacted');
            li.add(ld);
            
            
        }
        update li;
        
    }
    static testmethod  void TestJobSchedule(){
        string sch = '0 30 12 2 20 ?';
        Test.startTest();
        string jobid=System.Schedule('SchedulableClass',sch, new LeadMaintanace());
        List<Lead> li =[Select Id from Lead where LeadSource=null limit 10];
        system.assertEquals(10,li.size());
        test.stopTest();
        
    }
    
    
}
Best Answer chosen by Shruthi MN 19
Ravi Dutt SharmaRavi Dutt Sharma
Hi Sruthi,

Please use below class and check the output
 
@isTest
public class TestLeadMaintanace {
    @testsetup
    static void setup(){
        List<Lead> li = new List<Lead>();
        for(Integer i;i<10;i++){
            Lead ld = new Lead(Company = 'Proseraa',LastName ='MN',Status = 'Working - Contacted');
            li.add(ld);
        }
        insert li;
        
    }

    static testmethod  void TestJobSchedule(){
        string sch = '0 30 12 2 20 ?';
        Test.startTest();
        string jobid=System.Schedule('SchedulableClass',sch, new LeadMaintanace());
        Test.stopTest();
        List<Lead> li =[Select Id from Lead where LeadSource='Web'];
        system.assertEquals(10,li.size()); 
    }
    
    
}