+ Start a Discussion
AngelikaAngelika 

How to determine a test class for apex scheduler

I am new to apex and built an apex schedule class that runs everyday. If the account review date for commisions is two weeks (14 days) away the scheduler will send an email to our Sales Department.

I have scheduled the class through the apex scheduler (NOT system.schedule)

How do I test my code?

Here is my schedulable class:

global class AccountReviewSchedulerOtherObjectID implements Schedulable{
global void execute (SchedulableContext ctx)
{

sendEmail();

}
public void sendEmail()
{


for(Account acc : [SELECT Id FROM Account WHERE Next_Account_Review_Date__c = : system.Today().addDays(14)])
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId('00XF0000000LfE0');
mail.setTargetObjectId('005J0000000JWYx');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });
}
}
}

To test the class would it be:

test.starttest();
AccountReviewSchedulerOtherObjectID sco = new scheduledCreateOpportunity();
test.stopTest();

If so, where would I put this class (what would I call it) and how would I schedule it?

 

Though my schedule class has a submited, start and next scheduled run on "Scheduled Apex Jobs"......no email is sent out. How do I test if the email method works? The test class? 

 

What exactly does a test class do besides providing test coverage? 


 

 

tomcollinstomcollins

The Test class is used for automated tests, to ensure that you don't break anything when making changes to the tested class.

 

My understanding is that for your test class, you need to create fake Account records that will trigger the code in the tested class.  In your case, the test should create an Account record with a date set to 2 weeks from today, and then call sendEmail().  Unless the platform doesn't actually send emails when running tests (much like it doesn't make outbound HTTP requests), you should get a message when the test runs.

 

Have you tried manually running your class from the developer console to see if it's executing the code inside your for loop?

 

Shouldn't the code inside the for loop be using the "acc" Account object's ID in the mail.setTargetObjectId() method?  Maybe mails aren't going out because your hard-coded ID is invalid?

RiojinRiojin

Hi,  this link have a example about Scheduled  test class

 

 

 http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm