2

I am running a test on a batchable class.

@isTest

public class BatchSkillExcalationUpdaterTest {
    static testMethod void ValidateUpdates() {

        ...
        EscalationSkillUpdater SkillBatch = new EscalationSkillUpdater();    
        system.debug(logginglevel.FINEST, 'BATCHING...');
        Database.executeBatch(SkillBatch);
        system.debug(logginglevel.FINEST, 'FINISHED BATCHING.');
        ...
    }
}

The class I am testing:

global class EscalationSkillUpdater implements Database.Batchable <sObject> {
    global Database.QueryLocator start (Database.BatchableContext BC) {
        ...        
        return Database.getQueryLocator(query);
    }

    global void execute (Database.BatchableContext BC, list<SkillEscalationChangeRecord__c> Data) {
        ...
        System.debug(logginglevel.FINEST, 'FLAG');
        update Data;
    }

    global void finish (Database.BatchableContext BC) {}
}

However, the Debug message in the execute() method is not showing up:

enter image description here

The 'FLAG' from the batchable class is not showing up. Why is this? Is there another way to "print" values to examine?

3
  • 1
    Are you sure the query is returning results? If there are no records returned then the execute method will NOT well execute
    – Eric
    Commented Sep 24, 2015 at 20:14
  • I'm not sure the query is returning result, that actually what I'm trying to debug. I'm getting the same issue even when the System.debug() is the first line in the start() method.
    – Will
    Commented Sep 24, 2015 at 20:18
  • remember in test methods you have to created the appropriate data
    – Eric
    Commented Sep 24, 2015 at 20:22

2 Answers 2

5

You should call Test.startTest(); before calling Database.executeBatch(SkillBatch);. Then call Test.stopTest(); afterwards to have the actual batch run.

From Adding a Test for the Batch Apex Class:

The call to Database.executeBatch is included within the Test.startTest and Test.stopTest block. This is necessary for the batch job to run in a test method. The job executes after the call to Test.stopTest. Any asynchronous code included within Test.startTest and Test.stopTest gets executed synchronously after Test.stopTest.

So, without the start/stop calls your batch job will be queued, but will never run in the testing context as it is typically asynchronous.

Also, as Eric commented, ensure that you have inserted SkillEscalationChangeRecord__c records as part of the test for the batch query to find.

1
  • This seems to be the problem. I was calling test.starttest() before inserting my test objects and stoptest() after my assertions. Moving these to contain only the database.executeBatch() call, and it is not printing debug successfully. Thanks!
    – Will
    Commented Sep 25, 2015 at 14:41
2

The most logical answer:

The query in the start method is not returning results thus the execute method will NOT well execute and no debug will be shown there

or

To daniel's point below (I believe he will turn out to be having the correct answer)

You have to wrap the batch call in Test.StartTest and test.stopTest

(I forgot you posted your test class....)

You must log in to answer this question.

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