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
Prakhar Singh 8Prakhar Singh 8 

Test Class for Batch Apex for better coverage

I am trying to write Test Class for the following Batch Apex but unable to get more than 40 percentage coverage 
 
global class AutoRenewContractBatchApex implements Database.Batchable<SObject>, Database.Stateful {          
          global Database.QueryLocator start(Database.BatchableContext bc){         
                 return Database.getQueryLocator( 'SELECT Id, RenewalQuoted__c, Owner_Expiration_Notice__c, EndDate FROM Contract WHERE RecordType.DeveloperName = \'Class_Contract\' AND Owner_Expiration_Notice__c != Null AND RenewalQuoted__c = FALSE' );     

uncovered  [    
global void execute(Database.BatchableContext bc, List<Contract> crlist){  
          for(Contract contract:crlist){ 
              integer dateDifferene=(date.today()).daysBetween(contract.EndDate);
             String ExpireDays = String.valueOf(dateDifferene);             
             system.debug('@@dateDifferene-----' +dateDifferene);                                                                                 system.debug('@@OwnerExpirationNotice-----' +contract.Owner_Expiration_Notice__c);          if((ExpireDays==contract.Owner_Expiration_Notice__c)){ 
            contract.RenewalQuoted__c = TRUE;                 
           Id contrId = contract.Id;                 
            update crlist;                                                                                       ]uncovered   
                                            }
                      }                    
 }     
   global void finish(Database.BatchableContext bc){                 

}
 
The Test Class is following through which I am geeting 33 percent coverage.
 
@isTest  public class AutoRenewContractBatchTest {     
@isTest     static void checkAutoRenewContract()     {         
Id crId = Schema.SObjectType.Contract.getRecordTypeInfosByDeveloperName(). get('Class_Contract').getRecordTypeId();                  
 
Account acc = new Account(Name = 'Test', Paperwork__c = 'Fuels Indian',
                           CurrencyIsoCode = 'IND' );         insert acc;         
        
List<Contract> crList = new List<Contract>();         
for(Integer i=0; i < 200; i++){                          
Contract contract = new Contract();             
contract.AccountId = acc.id;             
contract.RenewalQuoted__c = False;             
contract.BPG_Owner_Expiration_Notice__c = '';             
contract.RecordTypeId = crId;             
contract.StartDate = System.today();            
 contract.ContractTerm = 4;                          
crList.add(contract);       
  }         
insert crList;                 
 test.startTest(); 
                 AutoRenewContractBatchApex batch = new AutoRenewContractBatchApex();         database.executeBatch(batch);                 
 test.stopTest();     

}
 
//unable to insert EndDate field as it is a not writable field, EndDate is being calculated by StartDate+ContractTerm
 
It also have a scheduler 
 
global class AutoRenewContractSchedule implements Schedulable {    
 global void execute(SchedulableContext ctx) {        
 AutoRenewContractBatchApex batchObject = new AutoRenewContractBatchApex();  
Id batchId = Database.executeBatch(batchObject, 1);     
  }
}
 
Can anyone tell how what should I do to improve code coverage. I am also marking the uncovered portion of the code.
Best Answer chosen by Prakhar Singh 8
Shubham Jain 338Shubham Jain 338
Hi Prakhar,

Yes as per Ankalah use to debug and I guess Owner_Expiration_Notice__c is not been set that's why it is not returning anything. Just assign the value in the test class only to test and if it works then there is an issue in the trigger where you are updating the value or debugging the value in the start method.

Hope it helps.

Please mark this as the best answer if it helps.

Thanks
Shubham Jain

All Answers

Shubham Jain 338Shubham Jain 338
Hi Prakhar,

You have not assigned any value to Owner_Expiration_Notice__c in the test class and in batch, we are checking this field shouldn't be null in SOQL.


Please mark this as the best answer if it helps.

Thanks
Shubham Jain
AnkaiahAnkaiah (Salesforce Developers) 
Hi Prakar,

Can you share which lines of code was not covered?

Thanks!!
Prakhar Singh 8Prakhar Singh 8
Hi Shubham, for the Owner_Expiration_Notice__c value you can 90 as I have automated the through trigger flow.
AnkaiahAnkaiah (Salesforce Developers) 
Then you can remove that field from test data and try to run the test class.

If still not getting the coverage, pls share which lines of code not covering.

Thanks!!
Prakhar Singh 8Prakhar Singh 8
Hi Ankaiah, I have used big brackets Uncoverd[
                                                                                           ] uncovered
to show the portion of code not covered

from Line 15 -    global void execute(Database.BatchableContext bc, List<Contract> crlist){
to Line 25 -    update crlist;
exculding line -  system.debug('@@dateDifferene-----' +dateDifferene); and system.debug('@@OwnerExpirationNotice-----' +contract.BPG_Owner_Expiration_Notice__c);
 
AnkaiahAnkaiah (Salesforce Developers) 
Can you check the debug logs, if the below query returning the records or not 
'SELECT Id, RenewalQuoted__c, Owner_Expiration_Notice__c, EndDate FROM Contract WHERE RecordType.DeveloperName = \'Class_Contract\' AND Owner_Expiration_Notice__c != Null AND RenewalQuoted__c = FALSE' );
And also check on which condition  flow is updating the Owner_Expiration_Notice__c field and that condition was met or not.


Thanks!!
 
Shubham Jain 338Shubham Jain 338
Hi Prakhar,

Yes as per Ankalah use to debug and I guess Owner_Expiration_Notice__c is not been set that's why it is not returning anything. Just assign the value in the test class only to test and if it works then there is an issue in the trigger where you are updating the value or debugging the value in the start method.

Hope it helps.

Please mark this as the best answer if it helps.

Thanks
Shubham Jain
This was selected as the best answer
Prakhar Singh 8Prakhar Singh 8
Yes, Owner_Expiration_Notice__c was not returnning the value so I just used Owner_Expiration_Notice__c = '90'; and its workong fine.
Writing another test method to call schulder class, but facing issues. Can you help me out in tht?