+ Start a Discussion
Internal PartnerInternal Partner 

How to insert records for custom metadata types in a simple test class?

Hi all,

I am struggling with coding a test class for a class accessing custom metadata class.
The class tries to insert a record for a custom metadata type, but no idea if my code is going to somewhere. I could not find any concrete solution for my problem. Some say you need a wrapper class, some say JSON.

I would appreciate any guidance, help as I am not a developer myself, but unfortunately all developers are busy at the moment.

This is my class:
 
global class EmailReminder implements Schedulable {  
    
   global void execute(SchedulableContext SC) {
      sendEmail();
   }
    
    
    global void sendEmail(){

        Messaging.reserveSingleEmailCapacity(2);
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        // Strings to hold the email addresses to which you are sending the email.
        
        Email_Reminder_Setting__mdt query = [SELECT Subject__c, OWA_Label__c, Body__c ,ToEmailAddress__c FROM Email_Reminder_Setting__mdt LIMIT 1];
        
        String[] toEmailAddress = new String[] {query.ToEmailAddress__c};
        String Subject = query.Subject__c;
        String Body= query.Body__c;
        String Owa_Label= query.OWA_Label__c;
       
        
        // Assign the addresses for the To and CC lists to the mail object.
        
        mail.setToAddresses(toEmailAddress);
        
        // Use Organization Wide Address  
        for(OrgWideEmailAddress owa : [select id, Address, Displayname from OrgWideEmailAddress]) {
            if(owa.DisplayName.contains(Owa_Label)) {
                 mail.setOrgWideEmailAddressId(owa.id); 
            }
              
        } 
           
        // Specify the subject line for your email address.
        mail.setSubject(Subject);
        
        // Set to True if you want to BCC yourself on the email.
        mail.setBccSender(false);
        
        // Optionally append the salesforce.com email signature to the email.
        // The email address of the user executing the Apex Code will be used.
        mail.setUseSignature(false);
        
        // Specify the text content of the email.
        //mail.setPlainTextBody('Please configure this');
        
        mail.setHtmlBody(Body);
        
        // Send the email you have created.
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

This is my test class so far:
 
@isTest
public class EmailReminder_Test {
   
  
      @testVisible static List<Email_Reminder__mdt> customMetadata { 
      get {
        if ( customMetadata == null )
            customMetadata = [ SELECT ToEmailAddress__c, OWA_Label__c, Subject__c, Body__c FROM Email_Reminder__mdt ]; 
            return customMetadata;
      } 
      set; }
      
      Test.startTest();
      EmailReminder sendEmailJob = new EmailReminder();
      String sch = '00 30 20 5 2 ? *';
      system.schedule('sendEmailJob', sch, sendEmailJob);
      Test.stopTest();
    
    
    
}

I am getting the error:

Error: Compile Error: Expecting ')' but was: 'sendEmailJob' at line 16 column 23​​​​​​​

What am I doing wrong?. 

 
Best Answer chosen by Internal Partner
Raj VakatiRaj Vakati
You no need to insetr custom metadata in test class and it will avaibale by default and try this code
 
@isTest
public class EmailReminder_Test {
   
     private static testMethod void testEMailReminder() {
 
      
      Test.startTest();
      EmailReminder sendEmailJob = new EmailReminder();
      String sch = '00 30 20 5 2 ? *';
      system.schedule('sendEmailJob'+System.now(), sch, sendEmailJob);
      Test.stopTest();
    
    }
    
}

 

All Answers

Raj VakatiRaj Vakati
You no need to insetr custom metadata in test class and it will avaibale by default and try this code
 
@isTest
public class EmailReminder_Test {
   
     private static testMethod void testEMailReminder() {
 
      
      Test.startTest();
      EmailReminder sendEmailJob = new EmailReminder();
      String sch = '00 30 20 5 2 ? *';
      system.schedule('sendEmailJob'+System.now(), sch, sendEmailJob);
      Test.stopTest();
    
    }
    
}

 
This was selected as the best answer
Internal PartnerInternal Partner
Thank you very much Raj.
It worked! :).