You need to sign in to do that
Don't have an account?
Prakhar 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.
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.
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
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
Can you share which lines of code was not covered?
Thanks!!
If still not getting the coverage, pls share which lines of code not covering.
Thanks!!
] 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);
Thanks!!
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
Writing another test method to call schulder class, but facing issues. Can you help me out in tht?