+ Start a Discussion
nagasnagas 

Test Class for a Apex Scheduler Class

hi I have a ApexScheduler Class which need to be test inorder to
deploy can anybody suggest me the way to achieve it to get the
coverage. I am posting the code below

 

global class ScheduleTerritoryInactiveMemberCheck implements 
Schedulable { 
    global void execute(SchedulableContext SC) { 
        TerritoryInactiveMemberCheck bcc = new 
TerritoryInactiveMemberCheck(); 
          bcc.query = 'select id,name from territory'; 
             Database.executeBatch(bcc,200); 
  }
 
} 

 cheers,

naga

 

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

 

public static testMethod void testschedule() {

Test.StartTest();

ScheduleTerritoryInactiveMemberCheck sh1 = new ScheduleTerritoryInactiveMemberCheck();

String sch = '0 0 23 * * ?'; system.schedule('Test Territory Check', sch, sh1); Test.stopTest(); }

 

 

 

All Answers

dmchengdmcheng

Have you read the documentation?

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_scheduler.htm|StartTopic=Content%2Fapex_scheduler.htm|SkinName=webhelp

nagasnagas

offcourse i have read the documentation but still i didn't get it. I am looking for examples similar to my scenario

BritishBoyinDCBritishBoyinDC

 

public static testMethod void testschedule() {

Test.StartTest();

ScheduleTerritoryInactiveMemberCheck sh1 = new ScheduleTerritoryInactiveMemberCheck();

String sch = '0 0 23 * * ?'; system.schedule('Test Territory Check', sch, sh1); Test.stopTest(); }

 

 

 

This was selected as the best answer
nagasnagas

Thanks British i have tried your code its giving me an Test failure saying

 

System.StringException: Unexpected end of Expression

 

at line

 

system.schedule('Test Territory Check', sch, sh1);
nagasnagas

hi British got it it was  my bad with the spacings its working i appriciate your help.

 

BritishBoyinDCBritishBoyinDC

Just to confirm - you created this as a new method in the Scheduleable class?

 

I pretty much just cut and pasted it from my own class, so not sure why it is failing - try changing the job name?

MunmunMunmun

Hi can u send me the whole code.bcz i also tried but its not working

nanda_001nanda_001

Hey , gr8 man thanks a lot  for Post this code

Divya SDivya S

Hi,

,

this is my code:

 

global class ScheduledApex implements Schedulable {

    public static String CRON_EXP = '0 0 0 3 9 ? 2022';


    global void execute(SchedulableContext ctx) {
       
        List <cd__c> ccdl = [select Date__c,Document_Count__c,Subsidiary__c, Subsidiary__r.Country__c from cd__c where Date__c=:system.today()];
        Set <String> country= new Set<string>();
           for (cd__c cc:ccdl){
               country.add(cc.Subsidiary__r.country__c);   
           }
       
        List<Subsidiary__c>sub=[select id, country__c from Subsidiary__c];
        List<cd__c>insertLog=new List<cd__c>(); 
           for (Subsidiary__c s: sub) {
               if(!country.contains(s.country__c)){
                   cd__c cclog= new cd__c(
                        Date__c=system.today(),
                        Document_Count__c=0,
                        subsidiary__c=s.id,
                        Risk_Domain_Name__c=s.country__c
                    );
                    insertLog.add(cclog);
               }
            }
            insert insertLog;
            }

}

 

I tried your test class:

 

@istest

public static TestScheduled  void testschedule() {

Test.StartTest();
ScheduledApex sh1 = new ScheduledApex();      
 String sch = '0 0 23 * * ?';
        system.schedule('Test check', sch, sh1);
Test.stopTest();

}

 

I get error message like this - Error: Compile Error: unexpected token: 'TestScheduled' at line 3 column 14

 

Please help me in resolving this.

Thanks in advance !

BritishBoyinDCBritishBoyinDC

I think your syntax is slighty wrong

 

Try changing this

public static TestScheduled  void testschedule() {

 

@istest

public static void  testschedule() {

Test.StartTest();
ScheduledApex sh1 = new ScheduledApex();      
 String sch = '0 0 23 * * ?';
        system.schedule('Test check', sch, sh1);
Test.stopTest();

}

Siva Raghava Krishna GSiva Raghava Krishna G
Can anyone tell me How to write Test class for the below code?

global class AccountRevenueUpdateYTDCalculation_AC implements Schedulable{
 global void execute(SchedulableContext SC) {
 AccountRevenueUpdate_AC ar = new AccountRevenueUpdate_AC();
 ar.submitYTDCalculation();
 ar.calculateReportData();
 }
}
Ilya IvanovskiyIlya Ivanovskiy
@isTest
public class ScheduleTerritoryInactiveMemberCheckTest {
    
    @isTest static void executeTest(){
        
        SchedulableContext sc = null;
        ScheduleTerritoryInactiveMemberCheck tsc = new ScheduleTerritoryInactiveMemberCheck();
        tsc.execute(sc);
         
    }  
    
}
Here is an option when the test starts immediately.
You can also use this code for quick testing as an anonymus apex.
There it will look like:
SchedulableContext sc = null;
ScheduleTerritoryInactiveMemberCheck tsc = new ScheduleTerritoryInactiveMemberCheck();
tsc.execute(sc);
Paritosh Gorai 3Paritosh Gorai 3
This is my scheduler class
global class AgentPointCallOutClassScehdule implements Schedulable {
 
    global void execute(SchedulableContext ctx){      
        
        AgentPointCallOutClass apPropImport = new AgentPointCallOutClass();
        Database.executeBatch(apPropImport, 100);
    }
}
What is the test class for this scheduler class?
 
David Roberts 4David Roberts 4
Following Ilya's example, I'd guess:
AgentPointCallOutClassScehdule apcocs = new AgentPointCallOutClassScehdule();
SchedulableContext sc = null;
apcocs.execute(sc);
[I've kept your mis-spelling of 'Scehdule' in the class name].
 
Madhankumar KMadhankumar K
Hi David, can you suggest how to write test class for try-catch Scheduler