3

I've a batch class in which I pass query as well as few parameters in its constuctor, like:

 bclass1 b1 = new bclass1(query1, 'x') ;
 bclass1 b2 = new bclass1(query2, 'y ') ;
 bclass1 b3 = new bclass1(query2, 'z ') ;

Batch class execution depends on parameters like 'x', 'y' and 'z';

Now when i test it, I use similar logic with multiple execution in single method, like this :

Test.startTest();

   bclass1 b1 = new bclass1(query1, 'x') ;
  bclass1 b2 = new bclass1(query2, 'y ') ;
  bclass1 b3 = new bclass1(query2, 'z ') ;

   Database.executeBatch(b1);
   Database.executeBatch(b2);
    Database.executeBatch(b3);

   Test.stopTest();

Now my questions are :

  1. Does that mean, executions of 3 batch job will start altogether in test class ?
  2. Would it be better, if I do execution of each Database.execute in separate test method of its own or doing this in current way wouldn't cause any issue?

2 Answers 2

2

You can't run more than a single batch execution in a test method. You'll need three test methods total to cover a situation like this. For chaining batches, you have to abort early and rig a way for each batch phase to run independently.

1
  • Thanks sfdcfox. Just one thing : When I run my test class, it is running fine. Would like to know, what is the impact. Jun 11, 2015 at 7:25
1

It won't cause any issues if you execute multiple batch jobs in a single test method.you don't need to execution of each Database.execute in separate test method.

@isTest
private class Test_BatchClassForEscalation {

    static testMethod void myUnitTest() {
        SupplierPPM__c sp = new SupplierPPM__c();
        sp.Name = 'abc';
        sp.CurrencyIsoCode = 'EUR';
        sp.Operating_Site__c = 'cc';
        string str = PPMHelper.returnMonthInString((date.today()-30).month());
        sp.Month__c = str;
        insert sp;

        Escalation__c ec = new Escalation__c();
        ec.CurrencyIsoCode = 'EUR';
        ec.Notification_Contact_Emails__c = '[email protected]';
        ec.Escalation_Contact_Email__c = '[email protected]';
        ec.Site__c = 'cc';
        insert ec;

        database.executeBatch(new BatchClassForEscalation());
        database.executeBatch(new BatchClassForNotificationEmails());
        database.executeBatch(new DynamicBatchClassEscalation());
        database.executeBatch(new DynamicBatchClassForNotification());

    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .