-3

I'm new to test classes and I'm trying create one for an apex schedulable class I made. I read the documentation but don't completely understand it. I want to schedule the class through the Apex Scheduler UI. Here's my schedulable class:

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];  
       List<task> ta = new list<task>();
        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';

            ta.add(t);

            }
         }

     insert ta;    
     }
}

And here is my test class, it's a copy paste from the documentation plus what I think it needs to adapt to my schedulable class:

@istest
class TestClass {
    static testmethod void test() {
        Test.startTest();

        Account myaccount = new account();
        myaccount.Name='Scrum Diario The Influence';
        myaccount.Cliente_Activo__c=TRUE;
        User usuario = new User();
        usuario.IsActive=TRUE;
        usuario.Alias = 'vic';
        Insert usuario;
        Insert myaccount;

        String jobId = System.schedule('testBasicScheduledApex',
                                       TestScheduledApexFromTestMethod.CRON_EXP,
                                       new TestScheduledApexFromTestMethod());

        CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered,
                          NextFireTime
                          FROM CronTrigger WHERE id = :jobId];

        // Verify the expressions are the same
        System.assertEquals(TestScheduledApexFromTestMethod.CRON_EXP,
                            ct.CronExpression);

        // Verify the job has not run
        System.assertEquals(0, ct.TimesTriggered);

        // Verify the next time the job will run
        System.assertEquals('2022-09-03 00:00:00',
                            String.valueOf(ct.NextFireTime));
        System.assertNotEquals('testScheduledApexFromTestMethodUpdated',
                               [SELECT id, name FROM account WHERE id = :a.id].name);

        Test.stopTest();

        System.assertEquals('testScheduledApexFromTestMethodUpdated',
                            [SELECT Id, Name FROM Account WHERE Id = :a.Id].Name);

    }
}
4
  • 1
    So...what's your question?
    – Adrian Larson
    Commented Mar 27, 2017 at 19:07
  • 1
    What Adrian said, and you need to move that insert out of the for loop..
    – EricSSH
    Commented Mar 27, 2017 at 19:08
  • My question is why is this giving me 0% coverage. And about the insert inside the loop I need to create several tasks depending on the amount of users, one for each user. @AdrianLarson Commented Mar 27, 2017 at 19:45
  • You are not scheduling your Scrum_Diario class you are scheduling a class named TestScheduledApexFromTestMethod which I doubt exists and if it does not the test class would not have compiled
    – Eric
    Commented Mar 27, 2017 at 20:35

1 Answer 1

-3

Here is the schedulable class and test class for it.

    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;
            }
        }
    }
}

Test class:

@isTest(seealldata=false)
private class Scrum_Diario_isTest{
    public static String CRON_EXP = '0 0 0 15 3 ? 2022';
    @testsetup
    static void createtestdata(){

        Account myaccount = new account();
        myaccount.Name='Scrum Diario The Influence';
        Insert myaccount;
        system.assertnotEquals(null,myaccount.id);
    }
    static testmethod void test1(){
        Test.startTest();
        system.schedule('test job', CRON_EXP, new Scrum_Diario());
        Test.stopTest();
    }
}
1
  • 1
    Can you add a little explanation about how this solves the issue? I've removed your other answer since this one reproduces it with more.
    – Matt Lacey
    Commented Mar 28, 2017 at 5:30

You must log in to answer this question.

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