I have a problem in achieving 75% + code coverage for a test class which i have mentioned below please correct me where i'm going wrong. Now i'm getting 57% code coverage.

Here is the apex scheduler class

global class Batch_Customer_old_scheduler implements Schedulable{

    public static string sched = '0 0 0 1 4 ?'; 

    global static string scheduleMe(){
        Batch_Customer_old_scheduler SC = new Batch_Customer_old_scheduler();
        return System.schedule('Customer_Existing', sched,SC); 

    global void execute(SchedulableContext SC){
        Batch_Customer_old batch = new Batch_Customer_old();
        Id batchid = database.executeBatch(batch,50);

The test class which i have written is not checking "ScheduleMe()" method which is there is the above scheduler class.

here is my test class for the above scheduler

    private class Batch_Test_Customer_Existing_schedule {
        static testMethod void schedule_batch(){

            string CORN_EXP = '0 0 0 1 4 ?';
            Batch_Customer_old_scheduler test1 = new Batch_Customer_old_scheduler();
           // Database.executebatch(Batch_Customer_old_scheduler.scheduleMe());
           // system.schedule('my batch', CORN_EXP, test1);
            string jobid = system.schedule('my batch job', CORN_EXP, new Batch_Customer_old_scheduler() );

           CronTrigger ct = [select id, CronExpression, TimesTriggered, NextFireTime from CronTrigger where id = :jobId]; 
System.assertEquals(Batch_Customer_old_scheduler.sched, ct.CronExpression);
       System.assertEquals(0, ct.TimesTriggered);
       System.assertEquals('2014-04-01 00:00:00',string.valueof(ct.NextFireTime));   



The test class you have does not call the scheduleMe() method and so will not execute or cover that code. You need to have a test method that calls the method which will schedule the class to run and provide coverage.

As a side note - you should be careful hardcoding strings representing time stamps into code - after 1st April your test will fail and may hamper deployments for other developers working alongside that org.

EDIT Further detail on why test would fail

The assertion you have in your test is that the next fire time for the job will be 1st April 2014 as your CRON Expression asks to fire at midnight on the 1st April every year. If your test is run after the first of April this year then the next scheduled time will be 1st April 2015, 2016 etc. and so your assertion will fail. This can cause issues for other developers in your system as they will have to possibly fix your test to deploy their code.

  • sorry i din't get that point..!! "after 1st april your test will fail"....can u please ellaborate on that. – Sharat Jan 21 '14 at 11:26
  • I have added further explanation - it is not the main point but just an aside of the question to be helpful. The long and short is you should not hard code dates into test classes. – pbattisson Jan 21 '14 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.