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
PaulMacPaulMac 

TestClass for SingleEmailMessage

Hi,

 

Just looking for some help with how to set up a test class for a class that simply sends an email.

 

My Code:

 

global

 

class SendOpenCasesEmail_CompanyName implements Schedulable {

global

}

 

void execute (SchedulableContext SC){

public

{Messaging.SingleEmailMessage mail =

mail.setTargetObjectId(

void SendEmail()new Messaging.SingleEmailMessage();'0035000000RXXXc'); //email recipient id

mail.setTemplateId(

'00X5000000XXXgY'); //email template id

String[] ccaddress =

ccaddress.add(

ccaddress.add(

ccaddress.add(

ccaddress.add(

mail.setccAddresses(ccaddress);

mail.setWhatID(

new String[]{};'joe@abc.com');'jane@abe.com'));'0015000000XXXt4'); //account id (show cases for this account)

Messaging.sendEmail(

}

 

}

 

 

thank you!

new Messaging.SingleEmailMessage[] { mail });

 

 

Best Answer chosen by Admin (Salesforce Developers) 
PaulMacPaulMac

I was able to come up with a test class for my emailing class.

 

@isTest
private class TestEmailing_Account {

 static testMethod void myUnitTest() {
        test.startTest();
  SendOpenCasesEmail_Account sendEmail = new SendOpenCasesEmail_Account();
  String schedule = '0 0 23 * * ?';
  system.schedule('Nightly Update', schedule, sendEmail);
  test.stopTest();
    }
}

All Answers

Pradeep_NavatarPradeep_Navatar

Find below a sample code for the testclass for the apex code :

 

            class_name controller = new class_name();

            SchedulableContext SCt = new SchedulableContext();

            controller.execute(SCt);

            //Create a new single email message object

            Messaging.SingleEmailMessage objEmail = new Messaging.SingleEmailMessage();

 

            //Address

            objEmail.setToAddresses(new String[] {'email@somewhere.com'});

 

            // Subject

            objEmail.setSubject('PDF Test email');           

 

            //Content

            objEmail.setPlainTextBody('This is a test email');

 

            For more information you can refer the Apex documentation for Testing of Apex Scheduler.        

 

Did this answer your question? if so, please mark it solved.                                                            

PaulMacPaulMac

Pradeep, Thank you very much for your reply

 

I'm assuming I replace "class_name" with the name of my class.

 

However I get an error on this line:

 

 class_name controller = new class_name();

           SchedulableContext SCt = new SchedulableContext();

 

Error is: Type cannot be constructed: SchedulableContext

 

Any thoughts?

 

thanks,

Paul

TheThe

hi, have you solved the issue?  I am experiencing the same error message and don't really see any other threads on this topic

 

"

 
Error: Compile Error: Type cannot be constructed: SchedulableContext at line 43 column 29

"

PaulMacPaulMac

I was able to come up with a test class for my emailing class.

 

@isTest
private class TestEmailing_Account {

 static testMethod void myUnitTest() {
        test.startTest();
  SendOpenCasesEmail_Account sendEmail = new SendOpenCasesEmail_Account();
  String schedule = '0 0 23 * * ?';
  system.schedule('Nightly Update', schedule, sendEmail);
  test.stopTest();
    }
}

This was selected as the best answer