-2

I am getting null pointer exception while creating a test class for schedule apex

         static testMethod void testMethod2(){        
         test.startTest();    
         Settings__cPS = new ProcessSetting__c(name = 'Corre',Max_Process__c=10);
         insert ps;
         system.debug('Process' + ps.Process__c);
        BatchSchedule obj1 = new BatchSchedule();
         String sch = '0 0 10 * * ?';
         system.schedule('TestCheck', sch, obj1); 
         test.stopTest(); 
      }

Sechedue class for which i am trying to write test metho

global class CorreSchedule implements Schedulable{

    global static void execute(SchedulableContext SC){
        Settings__csetting =  Setting__c.getInstance('Corr);
        Integer oppsize = Integer.valueOf(setting.Process__c);
        final Integer Process = oppsize;
        BatchCorrespondence CNR = new BatchCorrespondence();      
        ID idBatch = Database.executeBatch(CNR,MaxProcess); 
    }
}

I am storing limit in custom setting

  • is there a typo - line 3 won't compile; please also identify where (line #) in this testmethod the null pointer exception occurs. See How to Ask for guidance – cropredy Dec 14 '19 at 6:34
  • Yes it might be typo there is no compile error – roshni Dec 14 '19 at 6:40
  • added sechdule class in question – roshni Dec 14 '19 at 6:41
  • error coming in line no 4 of sechule class – roshni Dec 14 '19 at 6:41
  • Please properly format the scheduleclass. Use edit and fix typo in test class. – cropredy Dec 14 '19 at 6:45
1

There are couple of issues with Batch and Test class

  1. Your class name doesn't matches with the name used in your test class
  2. You're not inserting the custom setting record with correct name which is used while retrieving the record.
  3. The API name Max_Process__c is used while instantiating the custom setting record however in the debug statement its only process__c.

Refer below corrected code:

global class CorreSchedule implements Schedulable{

    global static void execute(SchedulableContext SC){
        Settings__c setting =  Setting__c.getInstance('Corr);
        if (setting != null) {
           Integer oppsize = Integer.valueOf(setting.Max_Process__c);
           final Integer Process = oppsize;
           BatchCorrespondence CNR = new BatchCorrespondence();      
           ID idBatch = Database.executeBatch(CNR,Process); 
        }
    }
}

Test class:

static testMethod void testMethod2(){        

    Settings__c PS = new ProcessSetting__c(name = 'Corr',Max_Process__c = 10);
    insert ps;
    system.debug('Process' + ps.Max_Process__c);
    CorreSchedule obj1 = new CorreSchedule ();
    String sch = '0 0 10 * * ?';
    test.startTest();    
    system.schedule('TestCheck', sch, obj1); 
    test.stopTest(); 

 }
0

I think that your test class will not compile. Please find corrected code as per my understanding :

static testMethod void testMethod2(){        
         test.startTest();    
         Settings__c PS = new Setting__c(name = 'Corr',Process__c=10);
         insert ps;
         system.debug('Process' + ps.Process__c);
        BatchSchedule obj1 = new BatchSchedule();
         String sch = '0 0 10 * * ?';
         system.schedule('TestCheck', sch, obj1); 
         test.stopTest(); 
}

Also , error is seen on line 4 since , you are inserting custom setting with value as "Corre" in test class but in schedular you are fetching with name as "Corr". Please find above corrected method.

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.