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
Santosh Shah 4Santosh Shah 4 

vf email template test class

Can anyone help me to write test class for following 

apex class: 

public class Emailtemplatehandler {
   
    public static void Sendmailtocustomer(Account acc){
       
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        Messaging.SingleEmailMessage mail        = new Messaging.SingleEmailMessage();
       
        North_acc__c CityRec    = [select Email__c from North_acc__c where name_city =:acc.name_city];
        EmailTemplate et                         = [Select Id, Name from EmailTemplate where Name = 'VFMAIL'];
        list<string> sendTo                      = new list<string>();
       
       
        sendTo.add(CityRec.Email__c);
        mail.setToAddresses(sendTo);
        mail.setSenderDisplayName('Displyname');
        mail.setTargetObjectId(UserInfo.getUserId());
        mail.setWhatId(acc.Id);
        mail.setSaveAsActivity(false);
        mail.setTemplateId(et.Id);
        mails.add(mail);
        system.Messaging.sendEmail(mails);
    }
}
ShivankurShivankur (Salesforce Developers) 
Hi Santosh,

You can use code something like below:
@isTest    
Global class EmailtemplatehandlerTest {
    public static testmethod void testvalidate(){
        Account newAcc = new Account();
        newAcc.name_city='testcity';
        insert newAcc;

        North_acc__c nAcc = new North_acc__c();
        nAcc.Email__c = 'testemail@xxxx'
        nAcc.name_city='testcity';
        insert nAcc ;

        EmailTemplate et=new EmailTemplate();
        et.Name='VFMAIL';
        insert et;

        Test.startTest();
            Emailtemplatehandler.Sendmailtocustomer(newAcc);
        Test.stopTest();
    }
}
Please modify the code as per needs, its just rough code made according to the Apex class given.

For more guidelines on writing test class in Apex, follow below link:
https://trailhead.salesforce.com/content/learn/modules/apex_testing/apex_testing_intro
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.