[SalesForce] test class for batch apex not covering execute method

I have written a batch class for cloning the opportunity and updating the stage field. Class is working fine. But test class is not covering the execute method. Logically I felt the test class is correct. Please provide any suggestions. Thanks in advance.

enter image description here

Following is the code for cloning to be tested-

global class UpdateOpportunity implements Database.Batchable<sObject>{

string query;

global Database.querylocator start(Database.BatchableContext BC){

String oppRecordTypeID = Schema.getGlobalDescribe().get('Opportunity').getDescribe().getRecordTypeInfosByName().get('Small Group').getRecordTypeId();

Query = 'Select id, name,StageName,RecordTypeId,Type,Description,CloseDate,Amount,Reason_for_loss__c,Renewal_date__c From Opportunity where StageName = \'Closed Won\' AND RecordTypeId = :oppRecordTypeID AND Renewal_date__c != null ';
return Database.getQueryLocator(query);
}


global void execute(Database.BatchableContext BC, List<Opportunity> scope1){
List<Opportunity> list1 = new List<Opportunity>();
Date t = Date.today();
for(Opportunity p : Scope1){ 
if(p.Renewal_date__c == t)
{ 
Opportunity opp=new Opportunity();
opp = p.clone();
opp.StageName = 'Qualification';
list1.add(opp); 
} 
} 

try {

insert list1;
} 
catch(Exception e) {
System.debug(e);
} 
}



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

Test Class –

@isTest
public class TestOpp
{
    static testMethod void BatchProcessAccount_TestMethod (){

         List<Opportunity> orglist = new List<Opportunity>();
         List<Opportunity> clonedlist = new List<Opportunity>();
         integer i = 0;

         for(i=0;i<5;i++)
         {
             Opportunity opp=new Opportunity();
                        opp.Name = 'opp' + i;
                        opp.StageName = 'Qualification';
                        opp.Type = 'New Customer';
                        opp.Description = 'test desc';
                        opp.CloseDate = Date.today();
                        opp.Renewal_date__c = Date.today();
                        opp.Amount = 2220;
                        opp.Reason_for_loss__c = 'abc';
                        opp.RecordTypeId = Schema.getGlobalDescribe().get('Opportunity').getDescribe().getRecordTypeInfosByName().get('Small Group').getRecordTypeId();
                        orglist.add(opp);
         }

        insert orglist;


        for(Opportunity p : orglist){ 
            Opportunity opp=new Opportunity();
            opp = p.clone();
            opp.StageName = 'Qualification';
            clonedlist.add(opp); 

        } 
        try{
            insert clonedlist;
        }
        catch(Exception e){
            system.debug(e);
        }

        Test.startTest();
        UpdateOpportunity tb = new UpdateOpportunity();
        Id batchId = Database.executeBatch(tb);
        Test.stopTest();

  }

}

Best Answer

If the execute() method isn't covered, that means the start() method returned an empty list.

Your start() method is:

Query = 'Select id, name, StageName, RecordTypeId, Type, Description,' + 
      ' CloseDate,Amount,Reason_for_loss__c,Renewal_date__c' + 
   ' From Opportunity ' + 
   ' where StageName = \'Closed Won\' AND ' + 
   ' RecordTypeId = :oppRecordTypeID AND ' + 
   ' Renewal_date__c != null ';
return Database.getQueryLocator(query);

Hence, your mock test data doesn't match the criteria.

So, how to diagnose?

  • after inserting the mock data in the test method, requery it and use system.debug to verify the values. This way you can check to see if any triggers or Process Builder/Flows have altered the data such that it no longer meets the start() method filter.
Related Topic