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
TheLearnerTheLearner 

Test class for email

HI Experts,

Could any one write test class for this please. thanks in advance.

public class TW_GeneralEmailDetails {
    public void setEMailDetails(String strToAddress, String strSubject, String strPlainTextBody){
        Messaging.Singleemailmessage email = new Messaging.Singleemailmessage();
        
        //Set Email Subject
        email.setSubject(strSubject);
        
        //Set ToAddress
        List<String> lstToAddresses;
        String[] toAddresses;
        if(strToAddress!=''&& strToAddress!=null){
          if(strToAddress.contains(',')){
            lstToAddresses=strToAddress.split(',');
            toAddresses = new String[] {lstToAddresses.get(0),lstToAddresses.get(1)};
          }
          else{
            toAddresses = new String[] {strToAddress};
          }
        }
        
        //String[] toAddresses = new String[] {strToAddress.split(',')};
        
        email.setToAddresses(toAddresses);
        //CH01.NEW.START -- Added empty list when there is no cc
        //Set CCAddress
        email.setCcAddresses(new List<String>());
        //CH01.NEW.END
        //Set Plain Text Body
        email.setPlainTextBody(strPlainTextBody);
        
        //Send it, ignoring any errors (bad!)
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }

    public void setEMailDetails(String strToAddress, String strSubject, String strCcAddress, String strPlainTextBody){
        Messaging.Singleemailmessage email = new Messaging.Singleemailmessage();
        
        //Set Email Subject
        email.setSubject(strSubject);
        
        //Set ToAddress
        List<String> lstToAddresses;
        String[] toAddresses;
        if(strToAddress!=''&& strToAddress!=null){
          if(strToAddress.contains(',')){
            lstToAddresses=strToAddress.split(',');
            toAddresses = new String[] {lstToAddresses.get(0),lstToAddresses.get(1)};
          }
          else{
            toAddresses = new String[] {strToAddress};
          }
        }
        
        //String[] toAddresses = new String[] {strToAddress.split(',')};
        
        email.setToAddresses(toAddresses);
        
        //Set CCAddress
        String[] ccAddresses = new String[] {strCcAddress };
        email.setCcAddresses(ccAddresses);
        
        //Set Plain Text Body
        email.setPlainTextBody(strPlainTextBody);
        
        //Send it, ignoring any errors (bad!)
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
    
Best Answer chosen by TheLearner
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class.
@isTest 
public class TW_GeneralEmailDetailsTest 
{
 static testMethod void testMethod1() 
 {
	 Test.StartTest(); 
		  TW_GeneralEmailDetails objController = new TW_GeneralEmailDetails();
		  objController.setEMailDetails('abc@test.com,xyz@test.com','Test Subject','strPlainTextBody');
		  objController.setEMailDetails('abc@test.com,xyz@test.com','Test Subject','aa@test.com','strPlainTextBody')
	 Test.StopTest();
 }
}

Please mark this as solution if this will help you.
Thanks
Amit Chaudhary
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below test class.
@isTest 
public class TW_GeneralEmailDetailsTest 
{
 static testMethod void testMethod1() 
 {
	 Test.StartTest(); 
		  TW_GeneralEmailDetails objController = new TW_GeneralEmailDetails();
		  objController.setEMailDetails('abc@test.com,xyz@test.com','Test Subject','strPlainTextBody');
		  objController.setEMailDetails('abc@test.com,xyz@test.com','Test Subject','aa@test.com','strPlainTextBody')
	 Test.StopTest();
 }
}

Please mark this as solution if this will help you.
Thanks
Amit Chaudhary
 
This was selected as the best answer
Eswar Prasad@Sfdc11Eswar Prasad@Sfdc11
HI Learner,
Try this below code it will work fine
@istest
public class TW_GeneralEmailDetailstest{
public static  testmethod void checkTest(){
String strToAddress='eswar@gmail.com';
String strSubject='checktest';
String strPlainTextBody='letme check details';
String strCcAddress='prasad@gmail.com';
TW_GeneralEmailDetails ge=new TW_GeneralEmailDetails();
test.start();
ge.setEMailDetails( strToAddress,  strSubject,strPlainTextBody);
ge.setEMailDetails(strToAddress,  strSubject,  strCcAddress, strPlainTextBody);
test.stop();
}
}

If you get the answer, please mark it as the correct answer. It will be a help to others who are facing the same problem later.

Regards,
Eswar Prasad