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
Hannah CampbellHannah Campbell 

need help making a test class

public class EmailHandler {
 
    public static void sendEmail2(){
        // Create Email and Send
        Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
        msg.setCcAddresses(new String[] {'sample@sample.com'});
       
 msg.setTemplateId('email'); //Use the custom HTML Template
        msg.setTargetObjectId('003p000000Nklh9'); 
 msg.setSenderDisplayName('Administrator');
       
        msg.setSaveAsActivity(false); // so that it wont be saved as an activity
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { msg });
    }
}
sowmya Inturi 9sowmya Inturi 9
Hi,
Test class for the above class:

@isTest
public class Test_SimpleEmail {
    @isTest static void sendEmail(){
        Contact c= new Contact();
        c.LastName='Test Name';
        insert c;
        
        
        Profile p = [select id from profile where name LIKE '%Admin%'];
        User u = new User(Alias = 'standt', Email='test@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='testEx@testorg.com');
        insert u;
        system.runAs(u){
            EmailTemplate e = new EmailTemplate (developerName = 'test',FolderId=UserInfo.getUserId() , TemplateType= 'Text', Name = 'test'); // plus any other fields that you want to set
            
            insert e;
        }
        EmailHandler .sendEmail2();
        
    }
}

Thanks,
Sowmya.
Raj VakatiRaj Vakati
First of all your code is not correct .. 

You need to use the code like below 
 
public class EmailHandler {
    
    public static void sendEmail2(){
        
        Contact c= new Contact();
        c.LastName='hajsdhjasd Name';
        c.Email='asjkdkajshdhas@gmail.com';
        
        insert c;
        
        
        // Create Email and Send
        Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
        msg.setCcAddresses(new String[] {'sample@sample.com'});
        
        msg.setTemplateId([Select id from EmailTemplate where DeveloperName='YOUR EMAIL TEMPLATE'].Id); //Use the custom HTML Template
// Can be User or lead or contact for setTargetObjectId
        msg.setTargetObjectId(c.id); 
        msg.setSenderDisplayName('Administrator');
        
        msg.setSaveAsActivity(false); // so that it wont be saved as an activity
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { msg });
// delete the contact
        delete c ;
    }
}
Test Class
@isTest
public class Test_SimpleEmail {
    @isTest static void sendEmail(){
        Contact c= new Contact();
        c.LastName='Test Name';
        insert c;
        
        
        Profile p = [select id from profile where name LIKE '%Admin%'];
        User u = new User(Alias = 'standt', Email='test@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='testEx@testorg.com');
        insert u;
        system.runAs(u){
            EmailTemplate e = new EmailTemplate (developerName = 'test',FolderId=UserInfo.getUserId() , TemplateType= 'Text', Name = 'test'); // plus any other fields that you want to set
            
            insert e;
        }
        EmailHandler.sendEmail2();
        
    }
}

 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Hannah,

Please find below the sample Test Class.

Business Class:
public class EmailHandler {
    
    public static void sendEmail2(){
        // Create Email and Send
        Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
        msg.setCcAddresses(new String[] {'sample@gmail.com'});
        
        msg.setTemplateId('00X7F000000E3bl'); //Use the custom HTML Template 
        msg.setTargetObjectId('0057F0000023YQ3'); // ID of user
        msg.setSenderDisplayName('Administrator'); 
        
        msg.setSaveAsActivity(false); // so that it wont be saved as an activity
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { msg });
    }
}

Test Class:
@isTest
public class SendEmailTest {
    @isTest public static void Test() {
        Test.startTest();
        EmailHandler.sendEmail2();
        Test.stopTest();
    }
}

I hope it helps you.

Kindly let me inform if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas