You need to sign in to do that
Don't have an account?
PaulMac
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 idmail.setTemplateId(
'00X5000000XXXgY'); //email template idString[] 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 });
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
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.
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
hi, have you solved the issue? I am experiencing the same error message and don't really see any other threads on this topic
"
"
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();
}
}