0

I'm having difficulties creating a test class to cover all my code. My schedulable class is as follows

global class Scrum_Diario implements Schedulable {
    global void execute(SchedulableContext SC) {

    Account accts = [SELECT Id, Name, OwnerID from account where Name='Scrum Diario The Influence' ];
    List<User> usuario = [Select Id, Alias from user where IsActive=True];    
        for (User us: usuario)
        {
            if(us.Alias!='EBenc'||us.Alias!='PEF'||us.Alias!='arodas'||us.Alias!='vecheve'){
            Task t= new task();
            t.Subject       =   'Scrum Diario';
            t.ActivityDate  =   Date.today();
            t.Description   =   'Llevar a cabo el Scrum diario antes de las 9:30';
            t.Status        =   'Open';
            t.WhatId        =   accts.Id;
            t.OwnerId       =   us.Id;
            t.Priority      =   'Normal';

            insert t;

            }
        }


    }

}
1
  • 3
    Can you please post the code from your current test class and point where exactly the problem is? Mar 23, 2017 at 23:07

1 Answer 1

1

What I always do when testing classes that are schedulable is to never test the execute method itself.

Just create a public execute or run method, and call it directly from your code instead. You should be able to get 90%+ coverage without the headache from scheduling a job in Apex.

But if you are a perfectionist, you can achieve 100% coverage too. Scheduled classes can be tested using Test.startTest() and Test.stopTest() on your test method. When you schedule your class to run, say, in 5 minutes, when you call Test.stopTest() it will run despite being scheduled to run 5 minutes from now. You just need to remember to set up your test data as usual before scheduling the class and calling stopTest.

In your example, you'd need to have a test class that sets up some data (if needed), and then call the System.schedule method, which takes as parameters the following:

  • a String that names the scheduled job (this name is what you'll see on Setup > Scheduled Jobs interface);
  • a CRON expression, that represents when the class should run;
  • an instance of the class you want to schedule;

Example:

System.schedule('Scrum Schedulable', '0 7 * * *', new Scrum_Diario());

Please also refer to the documentation on Apex Scheduler.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .