+ Start a Discussion
Harsha ShriHarsha Shri 

Need help in test class for Messaging.Singleemailmessage and meta data

Hi All,
I have written a apex class to send email. I am new to test classes. I want to write test class for this. Please suggest how to cover my apex class.
This is my code.
public class UtilityCont{
    
    public static List<cotacts1__c> PSEContacts = new List<cotacts1__c>();
  
    public static void EmailtoSRRTTeam(Instal_Prod__c IP) {
        try{
            
            User usr = [select Id, username from User where Id = :UserInfo.getUserId()];
            Account acc = [Select ShippingCountry from Account where id =: IP.Company__c];
            PSEContacts = [Select Id,Email__c from cotacts1__c where Countries_in_Cluster__c =: acc.ShippingCountry];
            
            EmailTemplate templateId = [Select id from EmailTemplate where name = :System.Label.SRRTEmailTemp];
            
            if(usr!=null) {
                
                Messaging.Singleemailmessage mail = new Messaging.Singleemailmessage();
                
                list<String> toEmailIDs = new list<String>();
                Integer i=0;
                for (cotacts1__c singleCCEmail: PSEContacts) {
                    toEmailIDs.add(singleCCEmail.Email__c);
                    
                    mail.setTargetObjectId(userinfo.getUserId());
                    
                    mail.setToAddresses(toEmailIDs);
                    mail.setTreatTargetObjectAsRecipient(false);
                    mail.setTemplateID(templateId.Id); 
                    mail.setWhatId(IP.Id);
                    mail.setSaveAsActivity(false);
                    
                }
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                
            }
        } catch(Exception ex) {

        }
    }
    public static String getUserContactNumber(){
        
        ContactNumber__mdt record;
        try {
          
            String userLocale = UserInfo.getLocale();
            String countryCode=  userLocale.substring(3,5);
            record= [SELECT MasterLabel,DeveloperName,ContactNumber__c FROM ContactNumber__mdt where DeveloperName=:countryCode]; 
        } catch(Exception ex) {
         
        }
        return record.ContactNumber__c;
    }
    
}

Thanks in Advance
Please help me​