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
ram sahuram sahu 

Test class for Apex scheduler class

Hi All,

Please guide me in writting Test code for below code:

Code :

global class userSignoutScheduler implements Schedulable {
    //List<Business_Hours_Reference_Setup__mdt> businessHoursMDList = new List<Business_Hours_Reference_Setup__mdt>();
    List<String> timeZoneKeyList = new List<string>();
    global void execute(SchedulableContext SC) {
        List<User_Availability__c> userAvailList = new List<User_Availability__c>();
        for(Business_Hours_Reference_Setup__mdt bHRefList: [Select id, Business_Hours_Id__c, Timezone_Key__c from Business_Hours_Reference_Setup__mdt where Record_Type__c='User']){
            if(BusinessHours.isWithin(bHRefList.Business_Hours_Id__c, {
            for(User_Availability__c userAvailRec: [Select id, User__c, LD_Availability_User__c from User_Availability__c where LD_Availability_User__c=TRUE AND User__r.timezonesidkey in :timeZoneKeyList]){
            update userAvailList;
Raj VakatiRaj Vakati
Try this code and add other required fields
public class userSignoutSchedulerTest{  

    public static testmethod void first1(){
		 Profile pf= [Select Id from profile where Name='System Administrator']; 
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
        insert uu;
		User_Availability__c  ut = new User_Availability__c () ; 
		ut.User__c =uu.Id ; 
		ut.LD_Availability_User__c =true;
		// add other fields 
		insert ut ; 
        Datetime dt =;
        String CRON_EXP = '0 '+ dt.minute() + ' * ' + + ' ' + dt.month() + ' ? ' + dt.year();
		userSignoutScheduler  sh1 = new userSignoutScheduler ();
		system.schedule('userSignoutSchedulerTest', CRON_EXP, sh1); 


Aman PathakAman Pathak
Hi Ram,

As are using custom Metadata, and it is not possible to replicate custom metadata type in your test class (as per my experience with metadata).

What you can do here is to include seeAllData= true annotation here (yes,I know it is not advisable but this isn't going to break unless you don't have that in other org as well).

Or Else,See this .

Please mark it best answer if it helped you.