4

In my handler class I schedule a batch class to run and update a field of records after Insert.

I have the below code to schedule the batch to run after insert happens:

public class sample{

public static void RunAfterInsert(List<sobject__c> obj){

   batchclassName batchjob = new batchclassName();
   System.scheduleBatch(batchjob,'A Batch Job ',1);


}

When I create some records in apex test class which invokes the after insert method to execute the batch. But I guess the batch is not getting scheduled so there is not update happening to the records as per my logic in Batch class.

I think the System.scheduleBatch(batchjob,'A Batch Job ',1); is not getting executed through test class. Is it correct? Any way I can make this run? But the batch class is getting covered 100% in through the test class

My test class:

@isTest
public class test{

@isTest
static void testmethod(){

    List<Employees__c> employeeList = new List<Employees__c>();
    for(Integer i=0;i<20;i++){ 
        Employees__c emp = new Employees__c(
            Name='Test'+i,
            Person_ID__c=String.valueof(i),
            ID__c = String.valueof(i) + 't',
            Employee_Status__c='Active'
        );
        employeeList.add(emp);
    }
    Insert employeeList;
}
  • 1
    Can you share the test class? – Sebastian Kessel Aug 30 '16 at 15:20
  • I believe , just inserting the records should take care of calling the after Insert method. – SfdcBat Aug 30 '16 at 15:24
  • Nope. scheduleBatch will schedule, but not run, the batch in question. You have to test both parts of your code separately. – Sebastian Kessel Aug 30 '16 at 15:26
  • Okay , Do I have to explicitly execute the batch using Database.executebatch() method here? – SfdcBat Aug 30 '16 at 15:28
  • No. Just test your batch separately, in a different class. You won't be able to go end-to-end on this use case. See @AdrianLarson's answer for more details – Sebastian Kessel Aug 30 '16 at 15:29
7

It's not going to run the batch. You should test the batch actions separately. In this case, you just need to query for the CronTrigger and verify that it did, indeed, get scheduled.

static testMethod void testAfterInsert()
{
    // setup data

    Test.startTest();
        insert data;
    Test.stopTest();

    system.assertEquals(1, [SELECT count() FROM CronTrigger],
        'A job should be scheduled');
}
static testMethod void testBatchExecution()
{
    // setup data
    insert data;

    Test.startTest();
        Database.executeBatch(new MyBatch());
    Test.stopTest();

    // assert on batch behavior
}
  • Okay and do I have to explicitly execute the batch using Database.executebatch() method here? – SfdcBat Aug 30 '16 at 15:30
  • You should test executeBatch in an entirely separate testMethod. – Adrian Larson Aug 30 '16 at 15:30

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.