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
TehNrdTehNrd 

testMethod help for class that implements Schedulable

The documentation is usually pretty helpful but in this case it only added to my confusion. How the heck do I test this very simple class?

global class ideaCleanSchedule implements Schedulable{
global void execute(SchedulableContext sc){
ideaCleanBatch job = new ideaCleanBatch();
ID batchprocessid = Database.executeBatch(job,100);
}
}

 I've been trying to do somethign like this with no luck.

Test.startTest(); ideaCleanSchedule job = new IdeaCleanSchedule(); //Line below as execut takes the argument of SchedulableContext but I'm stumped as to how to create this and pass it as an argument. job.execute(); Test.stopTest();

Thanks,

Jason


 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
TehNrdTehNrd
The response from salesforce.com is basically this.

A class that implements Schedulabe can't be covered with tests.

So it looks like you'll have 5-6 lines that aren't covered. This class should call another class in which all processing is done. Tests should be written for this separate class. This is what I did but I still have issues.....

http://community.salesforce.com/sforce/board/message?board.id=apex&thread.id=22074
Message Edited by TehNrd on 11-03-2009 11:59 AM

All Answers

TehNrdTehNrd
The response from salesforce.com is basically this.

A class that implements Schedulabe can't be covered with tests.

So it looks like you'll have 5-6 lines that aren't covered. This class should call another class in which all processing is done. Tests should be written for this separate class. This is what I did but I still have issues.....

http://community.salesforce.com/sforce/board/message?board.id=apex&thread.id=22074
Message Edited by TehNrd on 11-03-2009 11:59 AM
This was selected as the best answer
TehNrdTehNrd
Adding to the confustion is the fact that the example of testing this in the docs doesn't follow best practices and depends on existing data. This will most likely cause the test to fail if you copy and paste the example code into your org.
airrick3airrick3

I tried this and it got my test coverage to 100%  -->  http://community.salesforce.com/t5/Apex-Code-Development/Apex-Scheduler-Test/m-p/176917

 

This is what my code looks like

 

global class NightlyUpdate implements Schedulable{
   global void execute(SchedulableContext SC) {
   	AccountClosed.CloseChildern(); 
   }
}

 

@isTest
private class NightlyUpdateTest {
  static testMethod void myUnitTest() { test.startTest(); NightlyUpdate nightlyUpdate = new NightlyUpdate(); String schedule = '0 0 23 * * ?'; system.schedule('Nightly Update', schedule, nightlyUpdate); test.stopTest(); } }

 I didnt really care it what was executed in this test, because I have another test class that tests everything in the AccountClosed class