[SalesForce] Test class for batch is not covering the execute method

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.

Best Answer

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.

Related Topic