0
global class Animals implements Schedulable {
    global void execute(SchedulableContext SC) {
        Selector.getAnimals();
        Calculator.setAnimals();
    }
}

And I want to perform the following test:

@isTest static void testCACKPIs() {
    SchedulableContext SC = new SchedulableContext();
    Animals.execute(SC);
}

But it is not working, first the error message says:

Non static method cannot be referenced from a static context

  • It helps if you format your code so it appears as code. I've done this for you this time, but I would like to see you start to do this. The help page on formatting is a good reference. – Derek F Feb 12 at 12:16
1

The global modifier doesn't change the fact that your execute() method is still an instance method.

ClassName.method() is how static methods are called.

Since execute() doesn't have the static modifier (and I don't think it should have that modifier anyway), you'll need to create an instance of your class to be able to call the instance method.

e.g.

MyClass classInstance = new MyClass();
classInstance.instanceMethod();

How do I test asynchronous apex is also going to be a good resource for you in writing this test.

| improve this answer | |
0

I did finally like that:

global class Animals implements Schedulable {
**public static String CRON_EXP = '0 0 * * * ? *';** 
    global void execute(SchedulableContext SC) {
        Selector.getAnimals();
        Calculator.setAnimals();
    }
}

Adding this:

@isTest static void testAnimals() {
       Test.startTest();
          String jobId = System.schedule('testBasicScheduledApex',
          Animals.CRON_EXP, 
             new CAC_KPIs_Infomart());
          // Get the information from the CronTrigger API object
       CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime 
       FROM CronTrigger WHERE id = :jobId];
       Test.stopTest();

       }
| improve this answer | |

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.