You need to sign in to do that
Don't have an account?

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, system.now())) {
timeZoneKeyList.add(bHRefList.Timezone_Key__c);
}
}
if(timeZoneKeyList.size()>0){
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]){
userAvailRec.LD_Availability_User__c=FALSE;
userAvailList.add(userAvailRec);
}
}
if(userAVailList.size()>0)
update userAvailList;
}
}
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, system.now())) {
timeZoneKeyList.add(bHRefList.Timezone_Key__c);
}
}
if(timeZoneKeyList.size()>0){
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]){
userAvailRec.LD_Availability_User__c=FALSE;
userAvailList.add(userAvailRec);
}
}
if(userAVailList.size()>0)
update userAvailList;
}
}
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 https://developer.salesforce.com/blogs/engineering/2015/05/testing-custom-metadata-types.html .
Please mark it best answer if it helped you.
thanks,
Aman