+ Start a Discussion

Tricky "scheduled" test class

Hi folks,


I have a slight issue with a class that's implemented by a schedulable class...


I'm struggling to write a test class that gets any coverage for a class implemented by some scheduled apex. The schedule part itself is fine, but the class it implements is getting little-to-no coverage. Could anybody offer me any pointers as to how to get good coverage for the class below?


public class EmailSalesOffice {
   public static void SendEmail(){
      List<Milestone1_Milestone__c> activeproc = new List<Milestone1_Milestone__c>([Select id, Project__r.Sales_Office_Lead__r.Email, Project__r.Sales_Office_Lead__r.Name, Name, Project__r.Owner.Email, Kickoff__c, Project__r.Name, Deadline__c from Milestone1_Milestone__c where Deadline__c<=TODAY AND Kickoff__c>=TODAY]);
      String orgWideAddID = [select Address from OrgWideEmailAddress][0].Address;
       String[] address = new String[]{};
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
           for(integer i=0;i<activeproc.size();i++){
                system.debug('TO ADDRESSES: '+address);
                system.debug('REPLY TO ADDRESS: '+OrgWideAddID);
       	      	mail.setSenderDisplayName('Display Name');
       			mail.setSubject('Record of hours spent on "' +activeproc[i].Name+'" of "' +activeproc[i].Project__r.Name+'".');
       			mail.setPlainTextBody('Some text.');
       			List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.Email[] { mail });
       			System.debug('Email Sent: '+results.get(0).isSuccess() );


I've tried things like "System.assert(results.get(0).isSuccess());", but it doesn't seem to do anything to add to my coverage. I'm not after someone to write a test class for me, per se, but I'd like some "methodology" if you will for writing test classes surrounding email.







A few tips:


1. Your test method needs to wrap the invocation to the scheduled class with Test.startTest(), Test.stopTest(). stopTest() tells SFDC to execute the scheduled class so you can do asserts on the result of any database updates


2. In testmethods, emails are never sent; however Messaging.sendEmail will return a value


3. I presume you have a testmethod written directly on EmailSalesOffice (without testing through the apex scheduler).  In such a testmethod you will

  • create an instance of EmailSalesOffice eso = new EmailSalesOffice();
  • insert a few Milestone1_Milestone test records;
  • invoke eso.sendEmail();

If you want to test that Messaging.sendEmail succeeded, you need to change the SendEmail() method to not be void and to return a value


Thanks for the tips, Eric. I've successfully written a few testMethods before, but this email one had me unstuck.


With regards your final comment, is that to say that "void" methods are untestable and that I need to change the EmailSalesOffice.SendEmail() method to something else so that it returns, say an integer?




Argh!!! Help!


If I try to invoke the eso.SendEmail() method in my test class, I'm met with the error that I can't invoke static methods...


If I remove the static aspect, I can't schedule the class...




Sorry, I should have been paying attention and didn't notice that SendEmail was a static method, use this instead: