function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sumit dsumit d 

code coverage in helper class of a trigger on case

Hi all, i have my helper class given below:-
public class BatchClosedResponse implements Database.Batchable<sObject> {

   public Database.QueryLocator start(Database.BatchableContext BC){
         string query = 'Select id, First_Response__c,'+ 
                            'ClosedDate ,'+ 
                            'OwnerId, CreatedDate, Status,'+ 
                            'Helper_Closed_Date__c, '+
                            'LastModifiedDate, '+
                            'Helper_First_Responce__c '+
                            'from case WHERE id = \'500m000000885qW\''; 
                            
       System.debug('query'+query);
      return Database.getQueryLocator(query);
   }
    public void execute(Database.BatchableContext BC, List<Case> scope){
        List<Case> caseList = new List<Case>();
         BusinessHours defaultBH = [ SELECT Id 
                                   FROM BusinessHours 
                                   WHERE IsDefault = true  
                                   Limit 1
                                  ];
       for(Case caseObj : scope ){ 
         if( (caseObj.ClosedDate != Null)
                                          && caseObj.Status == 'Closed' 
                                         ){ 
                                             System.debug('I am here');
                                             Decimal lastResult = BusinessHours.diff( defaultBH.Id, caseObj.CreatedDate, caseObj.ClosedDate );
                                             
                                             Decimal lastResultingHours = lastResult/(60*60*1000);
                                             
                                             caseObj.Helper_Closed_Date__c= lastResultingHours;
                                             
                                         }
           caseList.add(caseObj);
                            }
        
        update caseList;
    }
    
    public void finish(Database.BatchableContext BC){

   }
    
}User-added imagehow to cover this part?
Any suggestion?
Musunuru SurekhaMusunuru Surekha
Hello,

In the SOQL query of start(), there is a record ID in where clause. When you try to test, it no records will be retrieved, as the test data inserted in test class will have a different record ID.
sumit dsumit d
what should i do now?
Any suggestion?
 
Musunuru SurekhaMusunuru Surekha
Try to remove the ID condition in where clause and use appropriate conditions if necessary.
Raj VakatiRaj Vakati
Update you code as shonw below and test class is here
public class BatchClosedResponse implements Database.Batchable<sObject> {

   public Database.QueryLocator start(Database.BatchableContext BC){
         string query = 'Select id, First_Response__c,'+ 
                            'ClosedDate ,'+ 
                            'OwnerId, CreatedDate, Status,'+ 
                            'Helper_Closed_Date__c, '+
                            'LastModifiedDate, '+
                            'Helper_First_Responce__c '+
                            'from case'; 
                            
       System.debug('query'+query);
      return Database.getQueryLocator(query);
   }
    public void execute(Database.BatchableContext BC, List<Case> scope){
        List<Case> caseList = new List<Case>();
         BusinessHours defaultBH = [ SELECT Id 
                                   FROM BusinessHours 
                                   WHERE IsDefault = true  
                                   Limit 1
                                  ];
       for(Case caseObj : scope ){ 
         if( (caseObj.ClosedDate != Null)
                                          && caseObj.Status == 'Closed' 
                                         ){ 
                                             System.debug('I am here');
                                             Decimal lastResult = BusinessHours.diff( defaultBH.Id, caseObj.CreatedDate, caseObj.ClosedDate );
                                             
                                             Decimal lastResultingHours = lastResult/(60*60*1000);
                                             
                                             caseObj.Helper_Closed_Date__c= lastResultingHours;
                                             
                                         }
           caseList.add(caseObj);
                            }
        
        update caseList;
    }
    
    public void finish(Database.BatchableContext BC){

   }
}
 
@isTest
private class BatchClosedResponseTest {
    @testSetup 
    static void setup() {
         List<Case> lstCase = new List<Case>();
        
        for (Integer i=0;i<10;i++) {
           Case caseObj = new Case(Subject='Test', Status = 'New',ClosedDate =System.today()+10);
    lstCase.add(caseObj);
        }
        insert lstCase;
       
    }
    static testmethod void test() {        
        Test.startTest();
        BatchClosedResponse uca = new BatchClosedResponse();
        Id batchId = Database.executeBatch(uca);
        Test.stopTest();
         
    }
    
}

 
sumit dsumit d
its not covering this part now
if( (caseObj.ClosedDate != Null)
               && caseObj.Status == 'Closed' 
              ){ 
                  System.debug('I am here');
                  Decimal lastResult = BusinessHours.diff( defaultBH.Id, caseObj.CreatedDate, caseObj.ClosedDate );
                  
                  Decimal lastResultingHours = lastResult/(60*60*1000);
                  
                  caseObj.Helper_Closed_Date__c= lastResultingHours;
                  
              }
            caseList.add(caseObj);
        }
        
        update caseList;
Any suggestion to cover this part?