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
VSK98VSK98 

How to write a Test class for a schedule class calling from button

Hi, 

I am struck for writing test class for below requirement.........Actaually i am schedule class from custom button

Here is my code
global class class_name{
  
 
  
    WebService static String Method_name()
    
    {
      
            Datetime currentTime = datetime.now();  
            Datetime newTime = currentTime.addSeconds(20);  
            // build cron expression  
            String sch= String.valueOf(newtime.second()) +' ' + String.valueOf(newtime.minute()) + ' ' + String.valueOf(newTime.hour()) + ' ' + String.valueOf(newTime.day()) + ' ' + String.valueOf(newTime.month()) + ' ? ' +  String.valueOf(newTime.year());  
            ScheduleClass_Name scheduleObject = new ScheduleClass_Name();  
           Id sId = system.schedule('schdclass'+newtime , sch, scheduleObject ); 
            
            return sId ;
       
             
    }
   
   }

So far i covered 0 percent .............Pls help me out.........

Adv Thnx
Siv
 
SRKSRK

can you please share the test class you have written so far to cover these lines ?

 

 

VSK98VSK98
Sure .................Here is my code
@isTest

public class class_name{
        
   
    static testMethod void Method_name(){
   
   Test.StartTest(); 
   Datetime currentTime = datetime.now();  
            Datetime newTime = currentTime.addSeconds(20);  
            // build cron expression  
            String sch= String.valueOf(newtime.second()) +' ' + String.valueOf(newtime.minute()) + ' ' + String.valueOf(newTime.hour()) + ' ' + String.valueOf(newTime.day()) + ' ' + String.valueOf(newTime.month()) + ' ? ' +  String.valueOf(newTime.year());  
            ScheduleClass_Name scheduleObject = new ScheduleClass_Name();  
           Id sId = system.schedule('schdclass'+newtime , sch, scheduleObject ); 
    Test.StopTest();
    
   }
  }

 
VSK98VSK98
Hi SRK,

I got it.................100% percentage covered......