function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Brad007Brad007 

Apex Scheduler test class

Hi, I have Scheduler class which is not initiating any other class but has a static method within. I had to build this class such a way as i had to make a apex call out and which needs to static. So my scheduler class works fine but i need a head start on how i can test the rest of the lines of code please check below for code and help me give a head start. /*The scheduler class fires hourly for the AP Files*/ global class APFiles_ScheduledHourlyRun implements Schedulable { global void execute(SchedulableContext sc) { APFilesMissing(); } @future(callout=true) public static void APFilesMissing() { // Exception log to capture various kind of errors as unable to make callouts, bad data or read time out issues. List errors = new List(); Datetime RuntimeCode = Datetime.newInstance(date.today(),time.newInstance(1, 11, 11, 0)); try { string ServiceResult = ''; // Creates list for Available Aupairs List APFiles = [Select a.Id ,a.Au_Pair__c,a.AP_Folder_Name__c, a.Photo4__c, a.Photo3__c, a.Photo2__c, a.Photo1__c, a.IsExist_Photo4__c, a.IsExist_Photo3__c, a.IsExist_Photo2__c, a.IsExist_Photo1__c,a.Au_Pair__r.Id,a.Au_Pair__r.AP_Status__c,a.Au_Pair__r.Record_Type_Name__c From AP_Application__c a where a.Au_Pair__r.AP_Status__c ='Available' AND a.Au_Pair__r.Record_Type_Name__c= 'Au Pair']; // Root of Xml string string XmlString = ''; //Create Xml record element for every APPhoto file with IsPhotoExist as false for(AP_Application__c APAppObj: APFiles) { if(APAppObj.IsExist_Photo1__c == false) { XmlString += ''; XmlString += ''+APAppObj.Id+''; XmlString += ''+APAppObj.Au_Pair__c+''; XmlString += ''+APAppObj.Photo1__c+''; XmlString += ''; } if(APAppObj.IsExist_Photo2__c == false ) { XmlString += ''; XmlString += ''+APAppObj.Id+''; XmlString += ''+APAppObj.Au_Pair__c+''; XmlString += ''+APAppObj.Photo2__c+''; XmlString += ''; } if(APAppObj.IsExist_Photo3__c == false ) { XmlString += ''; XmlString += ''+APAppObj.Id+''; XmlString += ''+APAppObj.Au_Pair__c+''; XmlString += ''+APAppObj.Photo3__c+''; XmlString += ''; } if(APAppObj.IsExist_Photo4__c == false ) { XmlString += ''; XmlString += ''+APAppObj.Id+''; XmlString += ''+APAppObj.Au_Pair__c+''; XmlString += ''+APAppObj.Photo4__c+''; XmlString += ''; } } //Close root Xml XmlString += ''; System.debug('XML:' +XmlString); // Create WSDL Stub for Call out GAPOrg.AjaxSoap servicestub = new GAPOrg.AjaxSoap(); servicestub.timeout_x = 9000; ServiceResult = servicestub.GAPAPFiles(XmlString); //System.debug('XmlResultfromService:' +xmldata); //Retrive the values from Xml using DOM class List Files = new List(); Dom.Document docx = new Dom.Document(); docx.load(ServiceResult); Dom.Xmlnode xroot = docx.getRootElement(); Dom.Xmlnode[] xrec = xroot.getChildElements(); for(Dom.Xmlnode child : xrec) { Files__c file = new Files__c(); for(Dom.Xmlnode node : child.getchildren()) { if(node.getName() == 'ApplicationId') { //system.debug('ApplicationId:' +node.gettext()); file.AP_Application__c = node.gettext(); } if(node.getName() == 'APID') { //system.debug('APID:' +node.gettext()); file.Contact__c = node.gettext(); } if(node.getName() == 'Filepath') { String[] path = node.gettext().split('/'); string filepath = +path[4]+'/'+path[5]+'/'+path[6]+'/'+path[7]; system.debug('Filepath:'+path[4]+'/'+path[5]+'/'+path[6]+'/'+path[7]); //system.debug('Filepath:' +node.gettext()); file.Path__c = filepath; } } Files.add(file); system.debug(File); } insert Files; } catch (Exception e) { Exception_Log__c ex = new Exception_Log__c(); ex.Exception_Code__c =''; ex.Error_Message__c = e.getMessage(); errors.add(ex); System.debug(e.getMessage()); } List nice = BusinessExceptionServices.SaveLogEntries(null,errors); errors.clear(); } } Thank in advance!
Chamil MadusankaChamil Madusanka

Your class is not clear. so I will provide you an example apex scheduler class and the test class of it.

 

//scheduler class

global class ActiveJDs implements Schedulable{ 

//Call Scheduled class & method
  global void execute(SchedulableContext SC) {
     ActiveJDHelper AJDH = new ActiveJDHelper();
     AJDH.checkActiveJD();
  }
}

 //test class

@isTest
private class test_ActiveJDs
{
    public static testMethod void testschedule() 
    {

        System.Test.StartTest();

        ActiveJDs sh1 = new ActiveJDs();
        String sch = '0 0 23 * * ?';
        system.schedule('Test Territory Check', sch, sh1);
        System.assert(true);
        System.Test.stopTest();

    }
}

 You need to set the scheduler in test class

 

Refer : http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

Brad007Brad007
/*The scheduler class fires hourly for the AP Files*/
global class APFiles_ScheduledHourlyRun implements Schedulable { 
     
    global void execute(SchedulableContext sc) 
            { 
                APFilesMissing(); 
            }

    @future(callout=true)  
    public static void APFilesMissing()
          {
            // Exception log to capture various kind of errors as unable to make callouts, bad data or read time out issues.
            List<Exception_Log__c> errors = new List<Exception_Log__c>();
            Datetime RuntimeCode = Datetime.newInstance(date.today(),time.newInstance(1, 11, 11, 0));
            try
              {      
                string ServiceResult = '';
                              
                // Creates list for Available Aupairs
                List<AP_Application__c> APFiles = [Select  a.Id ,a.Au_Pair__c,a.AP_Folder_Name__c, a.Photo4__c, a.Photo3__c, a.Photo2__c, a.Photo1__c, a.IsExist_Photo4__c, a.IsExist_Photo3__c, a.IsExist_Photo2__c, a.IsExist_Photo1__c,a.Au_Pair__r.Id,a.Au_Pair__r.AP_Status__c,a.Au_Pair__r.Record_Type_Name__c  From AP_Application__c a where a.Au_Pair__r.AP_Status__c ='Available' AND a.Au_Pair__r.Record_Type_Name__c= 'Au Pair'];    
                   
                   // Root of Xml string
                string XmlString = '<APFilesData>';    
                
                //Create Xml record element for every APPhoto file  with IsPhotoExist as false           
                for(AP_Application__c APAppObj: APFiles)
                  {    
                      if(APAppObj.IsExist_Photo1__c == false) 
                      {  
                        XmlString += '<APFiles>';
                        XmlString += '<ApplicationId>'+APAppObj.Id+'</ApplicationId>';
                        XmlString += '<APID>'+APAppObj.Au_Pair__c+'</APID>';
                        XmlString += '<Filepath>'+APAppObj.Photo1__c+'</Filepath>';              
                        XmlString += '</APFiles>';        
                      }
                      
                      if(APAppObj.IsExist_Photo2__c == false )
                      {
                        XmlString += '<APFiles>';
                        XmlString += '<ApplicationId>'+APAppObj.Id+'</ApplicationId>';
                        XmlString += '<APID>'+APAppObj.Au_Pair__c+'</APID>';
                        XmlString += '<Filepath>'+APAppObj.Photo2__c+'</Filepath>';              
                        XmlString += '</APFiles>';  
                      }
                      
                      if(APAppObj.IsExist_Photo3__c == false )
                      {
                        XmlString += '<APFiles>';
                        XmlString += '<ApplicationId>'+APAppObj.Id+'</ApplicationId>';
                        XmlString += '<APID>'+APAppObj.Au_Pair__c+'</APID>';
                        XmlString += '<Filepath>'+APAppObj.Photo3__c+'</Filepath>';              
                        XmlString += '</APFiles>';  
                      }
                      
                      if(APAppObj.IsExist_Photo4__c == false )
                      {
                        XmlString += '<APFiles>';
                        XmlString += '<ApplicationId>'+APAppObj.Id+'</ApplicationId>';
                        XmlString += '<APID>'+APAppObj.Au_Pair__c+'</APID>';
                        XmlString += '<Filepath>'+APAppObj.Photo4__c+'</Filepath>';              
                        XmlString += '</APFiles>';  
                      }
                  }
                    //Close root Xml
                    XmlString += '</APFilesData>';
                    System.debug('XML:' +XmlString);
                
                    // Create WSDL Stub for Call out
                    GAPOrg.AjaxSoap servicestub = new GAPOrg.AjaxSoap();
                    servicestub.timeout_x = 9000;
                    ServiceResult = servicestub.GAPAPFiles(XmlString);
                    //System.debug('XmlResultfromService:' +xmldata);
                        
                    //Retrive the values from Xml using DOM class          
                     List<Files__c> Files = new List<Files__c>();           
                     Dom.Document docx = new Dom.Document();
                     docx.load(ServiceResult);
                     Dom.Xmlnode xroot = docx.getRootElement();
                     Dom.Xmlnode[] xrec = xroot.getChildElements();
                           
                     for(Dom.Xmlnode child : xrec)
                       {
                         Files__c file = new Files__c();
                           
                           for(Dom.Xmlnode node : child.getchildren())
                           {
                               if(node.getName() == 'ApplicationId')
                               {
                                  //system.debug('ApplicationId:'  +node.gettext());
                                  file.AP_Application__c = node.gettext();
                               }
                               if(node.getName() == 'APID')
                               {
                                  //system.debug('APID:'  +node.gettext());
                                  file.Contact__c = node.gettext();
                               }
                               if(node.getName() == 'Filepath')
                               {
                                   String[] path = node.gettext().split('/');
                                   string filepath = +path[4]+'/'+path[5]+'/'+path[6]+'/'+path[7];
                                   system.debug('Filepath:'+path[4]+'/'+path[5]+'/'+path[6]+'/'+path[7]);
                                   //system.debug('Filepath:'  +node.gettext());
                                   file.Path__c = filepath;
                               }               
                              }
                           Files.add(file);
                           system.debug(File);
                       }
                       insert Files;
                }
                 catch (Exception e)
                   {
                     Exception_Log__c ex = new Exception_Log__c();
                     ex.Exception_Code__c ='';
                    ex.Error_Message__c = e.getMessage();
                    errors.add(ex);        
                      System.debug(e.getMessage());
                       }
            
                List<Exception_Log__c> nice = BusinessExceptionServices.SaveLogEntries(null,errors);
                   errors.clear();
                  
          }
          

          
}
Hi Sorry for the confusion. Thank you for your time. Here is the code that needs to be tested the static method below.