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
JoshTonksJoshTonks 

Need Help on My Test Class

Ive been writing a class that looks updates a tick box on a custom object. Ive written a test class for it but the the life of me I cannot figure out how to get my test class working.

So this is my Apex Class
global class KPMRefresh implements Schedulable{
  global void execute(SchedulableContext sc){
    List<Metric_Controller__c> lstofMC = [SELECT Id, Refresh_Metrics__c, Which_Month_Boolean__c, Active__c 
                                          FROM Metric_Controller__c 
                                          WHERE Which_Month_Boolean__c = true AND Active__c = true 
                                          LIMIT 200];

    List<Metric_Controller__c> lstofupdatedMC = new list<Metric_Controller__c>();
    if(!lstofMC.isEmpty()){
      for(Metric_Controller__c Id : lstofMC){
        lstofupdatedMC.add(id);
              Id.Refresh_Metrics__c = true;    
        }
             
      UPDATE lstofupdatedMC;
    }

  }
}
This is my test class.
@isTest
private class KPMRefreshTest {
  @testSetup
    static void setup(){
        List<Metric_Controller__c> lstofMC = new List<Metric_Controller__c>();
        for(Integer i = 1; i <= 200; i++){
            Metric_Controller__c Id = new Metric_Controller__c(Refresh_Metrics__c = false);
          lstofMC.add(id);
      }
        Insert lstofMC;   
    }
    
    static testmethod void testKPMRefreshScheduledJob(){
        String sch = '0 5 12 * * ?';
        Test.startTest();
        String jobId = System.schedule('ScheduledApexText', sch, new KPMRefresh());
        
        List<Metric_Controller__c> lstofMC = [SELECT Id, Refresh_Metrics__c FROM Metric_Controller__c WHERE Which_Month_Boolean__c = true AND Active__c = true LIMIT 200];
        
        system.assertEquals(200, lstofMC.size());
        Test.stopTest();   
    }   
}

Ive spent a few hours looking at this and cannot see what im missing. 
 
ramlakhanramlakhan
Hi Tonks,
I would suggest to move the assert after stop test. Unless stop test is not executed, you can not be sure if your scheduler had finished running.
Regards,
Lakhan
whtsup- +91-9028714167
Raj VakatiRaj Vakati
TRy this code
 
@isTest
private class KPMRefreshTest {
  @testSetup
    static void setup(){
        List<Metric_Controller__c> lstofMC = new List<Metric_Controller__c>();
        for(Integer i = 1; i <= 200; i++){
            Metric_Controller__c Id = new Metric_Controller__c(Refresh_Metrics__c = false ,Which_Month_Boolean__c=true ,Active__c =true );
          lstofMC.add(id);
      }
        Insert lstofMC;   
    }
    
    static testmethod void testKPMRefreshScheduledJob(){
        String sch = '0 5 12 * * ?';
        Test.startTest();
        String jobId = System.schedule('ScheduledApexText', sch, new KPMRefresh());
        
        List<Metric_Controller__c> lstofMC = [SELECT Id, Refresh_Metrics__c FROM Metric_Controller__c WHERE Which_Month_Boolean__c = true AND Active__c = true LIMIT 200];
        
      //  system.assertEquals(200, lstofMC.size());
        Test.stopTest();   
    }   
}