1

I have test class for a batch the test class is not covering the execute method of batch. Does anyone have any idea what is the reason for this. My batch class :

global class updateOwner_StatusNurtureBatch implements Database.Batchable<sObject>{

String Commercial_RecordTypeID = 'RecordTypeID'; // Commercial Record Type ID
String StatusName = 'StatusName'; // Status
String CommNutrure_QueueID = 'QueueID'; // COMM-NURTURE Queue ID
List<Lead> leadUpdate_Lsit = new List<Lead>(); // Lead list for update 
String soql;

String query = 'SELECT Ownerid From Lead Where RecordTypeID = :Commercial_RecordTypeID and Status = :StatusName and OwnerID != :CommNutrure_QueueID';

global updateOwner_StatusNurtureBatch(String query) {
    this.soql = query;
}    

global Database.QueryLocator start(Database.BatchableContext BC){
    return Database.getQueryLocator(soql);
}

global void execute(Database.BatchableContext BC, List<Lead> scope){
     system.debug('scope : ' + scope);

     //Loop through result 
     for(Lead l : scope){
        lead le = new lead();
        le.Id = l.id;
        le.Ownerid = CommNutrure_QueueID;
        leadUpdate_Lsit.add(le); 
     }

     system.debug(' Lead Update List ' + leadUpdate_Lsit);
     update leadUpdate_Lsit;
}

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

My test class for this code batch :

@isTest
public class updateOwner_StatusNurtureBatch_Test {

public static testmethod void updateOwner_StatusNurtureBatch_Test(){


    //List<Lead> lList = new List<Lead>();
    //lList.add(l);
   // lList.add(l1);

    string rID = 'RecordTypeID';
    String Sta = 'Status';
    String owner = 'ownerId';


    String query = 'SELECT Ownerid From Lead Where RecordTypeID = ';
    query += '\''+rID+ '\' and Status = ';
    query += '\''+Sta+'\' and OwnerID != ';
    query += '\''+owner+'\'';

    Test.startTest();
    updateOwner_StatusNurtureBatch updateOwnerRunBatch = new updateOwner_StatusNurtureBatch(query);
    ID resultID = Database.executeBatch(updateOwnerRunBatch, 2);  
    system.debug('Result ID : ' + resultID); 
    //updateOwnerRunBatch.execute(null, lList);     
    Test.stopTest();
}
}

What is wrong here why I am not able to cover the execute method. Please help me.

2 Answers 2

5

You need to have at least one record to process for the execute method to run. Since you're not using SeeAllData=true, you need to create at least one record that will meet the query criteria. You should create at least one record that would meet the criteria.

3
  • Hi @sfdcfox when I made seealldata=true. I am getting an error - "System.UnexpectedException: No more than one executeBatch can be called from within a testmethod. Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation. " I am not another calling any other batch from my current so what does this error mean.
    – Anu
    Commented Apr 3, 2015 at 6:18
  • It means more than batch size records were matched. SeeAllData should be considered dangerous. Make dummy leads in your test method, then execute your batch.
    – sfdcfox
    Commented Apr 3, 2015 at 6:33
  • Hi @Anu, if your total number of records is more than the batch size, then you will get this error. That is why we donot enable seealldata, and create only one or two records in test method and run the batch.
    – Tilak De
    Commented Apr 3, 2015 at 6:35
1

You are not creating any test data in your test method. you first have to understand the execution of batch class. the start method collects the records to process and passes these to execute method in batch (batch of 200 or user specified number). and after executing all batches, finish method will be executed. As the start method is not getting any record from query, your execute method is not getting executed. In order to resolve -

You have to either create some test data in your test class or make SeeAllData=true, but if you add SeeAllData=true, make sure the start method will collect less than 200 records from database and in turn the execute method is called only once.(you can restrict the number of records returned by start method by limiting query with Limit clause only for TEST execution. i.e. when isRunningTest is true.)

You must log in to answer this question.

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