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
Prince VenkatPrince Venkat 

Apex Emails Testclass

Hi
Below is my code can anyone help with a testclass for it for max code coverage.

public class LeadEmail
{
public static void SendEmailTolead(list<lead> lstLead)
{
    if(! lstLead.isEmpty())
   {
        list<messaging.SingleEmailMessage> lstEmails = new list<messaging.SingleEmailMessage>();
    for(lead lr : lstLead)
    {
        messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
        string[] toEmailsIds = new string[]{lr.Email};
        email.settoAddresses(toEmailsIds);
        
        email.setTargetObjectId( UserInfo.getUserId() );
        
        email.setSaveAsActivity( false );

        email.setSenderDisplayName('lead Record Creation Alert');
        
        email.setReplyTo('support@RFI.com');
        
        string emailsubject = 'congralutions'+lr.Name+'you have been registerd';
        
        email.setSubject(emailsubject);
        
        string emailHTMLContent = 'Dear'+lr.name+',<br/> <br/>'+
               '<br/><br/> here are your details... <br/><br/>'+
            
                '<br/> Your Company....'+lr.Company+
                '<br/> Your tile for the Role'+lr.Title+
                '<br/> your Status for The record'+lr.Status+
            'thanks & Regards';
        
        email.setHtmlBody(emailHTMLContent);
        
        lstEmails.add(email);
        
    }
   if(!lstEmails.isempty())
    {
        Messaging.sendEmail(lstEmails);    }
    
    }
}
}
Thanks in advance
Best Answer chosen by Prince Venkat
Suraj Tripathi 47Suraj Tripathi 47

Hi

Please find the Solution with 100% coverage.

@isTest
public class LeadEmailTest {
    public static testmethod void LeadEmailsTest(){ 
        List<Lead> lstLead =   new List<Lead>();
      Lead objLeads = new Lead( FirstName = 'Test', LastName = 'Test Data', Company = 'Testing' );  
        insert objLeads;
        lstLead.add(objLeads);
        Test.startTest();
        LeadEmail.SendEmailTolead(lstLead);
        
        Test.stopTest();
    }
}


Please let me know it is working or not?

Please mark it as the Best Answer so that other people would take reference from it.

Thank You

All Answers

Suraj Tripathi 47Suraj Tripathi 47

Hi

Please find the Solution with 100% coverage.

@isTest
public class LeadEmailTest {
    public static testmethod void LeadEmailsTest(){ 
        List<Lead> lstLead =   new List<Lead>();
      Lead objLeads = new Lead( FirstName = 'Test', LastName = 'Test Data', Company = 'Testing' );  
        insert objLeads;
        lstLead.add(objLeads);
        Test.startTest();
        LeadEmail.SendEmailTolead(lstLead);
        
        Test.stopTest();
    }
}


Please let me know it is working or not?

Please mark it as the Best Answer so that other people would take reference from it.

Thank You

This was selected as the best answer
CharuDuttCharuDutt
Hii Prince
Try Below Code
@isTest
public class LeadEmailTest {
    @isTest
    public static  void UnitTest(){ 
        List<Lead> lstLead =   new List<Lead>();
        for(Integer i=0;i<5;i++){
        Lead l = new Lead();
        l.FirstName = 'Test';
        l.LastName = 'lead';
        l.Email = 'Test'+i+'@test.com';
        l.Company='Tester';
        lstLead.add(l);
        }
        insert lstLead;
        Test.startTest();
        LeadEmail.SendEmailTolead(lstLead);
        
        Test.stopTest();
    }
}
Please mark it as the Best Answer So it helps other in future
Thank You