• Prakhar Singh 8
  • NEWBIE
  • 10 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
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.
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.