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';

try {

insert list1;
catch(Exception e) {

global void finish(Database.BatchableContext BC){

Test Class -

public class TestOpp
    static testMethod void BatchProcessAccount_TestMethod (){

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

             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();

        insert orglist;

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

            insert clonedlist;
        catch(Exception e){

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


  • Your exception handlers may be hiding underlying problems. I strongly recommend removing them. Swallowing exceptions is a very bad pattern. – David Reed Aug 28 '19 at 12:00

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 trigers or Process Builder/Flows have altered the data such that it no longer meets the start() method filter.
| improve this answer | |
  • 1
    Thanks your suggestion helped. The process builder was altering the data. – Mrunmayi Aug 29 '19 at 9:06

In Batch class you have query on opportunity object where StageName = 'Closed Won' and in test class you dont have any records of opportunity with the Stage field value as Closed Won.

In batch class execute method is not covered because the query is not returning any result to execute method.

| improve this answer | |
  • Tried changing the stage name to closed won but still not working – Mrunmayi Aug 28 '19 at 10:43
  • Please check whether the batch class is successfully executing without test class. – manish porwal Aug 28 '19 at 12:48
  • yes it is working – Mrunmayi Aug 29 '19 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.