You need to sign in to do that
Don't have an account?
Ayyagari Ramesh
Require help in test class as it is not covering the content in try catch blocks and resulting in only 34% coverage
Class:
global class ContractRenewalBatchJob implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
Date endDateContract = system.today() + Integer.valueOf(Label.KHCPQ_RenewalEmailDuration);
Date endDateContract1 = system.today() + Integer.valueOf(Label.KHCPQ_RenewalEmailDuration1);
Date endDateContract2 = system.today() + Integer.valueOf(Label.KHCPQ_RenewalEmailDuration2);
Date endDateContract3 = system.today() + Integer.valueOf(Label.KHCPQ_RenewalEmailDuration3);
String query = 'select id,Account.Name,Owner.Email,Owner.Name,Ownerid,EndDate,ContractNumber,CustomerSigned.id from Contract where (enddate=:endDateContract OR enddate=:endDateContract1 OR enddate=:endDateContract2 OR enddate=:endDateContract3) and (SBQQ__RenewalForecast__c=false and SBQQ__RenewalQuoted__c=false)';
List<sObject> sobjList = Database.query(query);
system.debug('ttt'+sobjList);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Contract> scope)
{
try
{
List<Messaging.SingleEmailMessage> myEmails = new List<Messaging.SingleEmailMessage>();
for(Contract c:scope)
{
system.debug('Contract : ' + c);
String body;
body = 'Hi ' + c.Owner.Name + ',<br/><br/>';
body = body + 'Contract ' + c.ContractNumber + ' is expiring on ' + c.EndDate.month() + '/' + c.EndDate.day() + '/' + c.EndDate.year() + '.<br/>';
body = body + '<br/>Please renew the Contract ASAP. You can access the contract in the URL below:<br/><br/>';
body = body + Label.WebsiteURL + '/' + c.id + '<br/><br/>';
body = body + 'Regards,<br/>AFH Sales Ops Team';
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setHTMLBody(body);
mail.setToAddresses(new String[]{c.owner.email});
mail.setSubject('ACTION REQUIRED: Your \"' + c.Account.Name + '\" Contract Requires Renewal');
myEmails.add(mail);
}
if(myEmails.size() > 0)
{
Messaging.sendEmail(myEmails,false);
}
}
catch(Exception ex)
{
system.debug('Exception : ' + ex.getMessage());
}
}
global void finish(Database.BatchableContext BC)
{
}
}
test class:
@isTest
public class ContractRenewalBatchJobTest
{
static testMethod void testMethod1()
{
User us = [Select id from User where Id = :UserInfo.getUserId()];
Date startDateWithinRange = system.today();
Date endDateWithinRange = system.today() + 29;
Date endDateWithinRange1 = system.today() + 59;
Date endDateWithinRange2 = system.today() + 89;
Date endDateWithinRange3 = system.today() + 119;
Account acc1 = KHCPQ_Test_DataFactory.createAccount('Parent Account','Active','Street 1','123');
Contract contract1 = KHCPQ_Test_DataFactory.createContract(acc1.Id,'Draft',startDateWithinRange,endDateWithinRange);
Contract contract2 = KHCPQ_Test_DataFactory.createContract(acc1.Id,'Draft',startDateWithinRange,endDateWithinRange1);
Contract contract3 = KHCPQ_Test_DataFactory.createContract(acc1.Id,'Draft',startDateWithinRange,endDateWithinRange2);
Contract contract4 = KHCPQ_Test_DataFactory.createContract(acc1.Id,'Draft',startDateWithinRange,endDateWithinRange3);
System.runAs(us)
Test.startTest();
ContractRenewalBatchJob obj = new ContractRenewalBatchJob();
DataBase.executeBatch(obj);
ContractRenewSchedule sch = new ContractRenewSchedule();
String cronStr = '0 0 0,12 * * ?';
system.schedule('testJob', cronStr, sch);
Test.stopTest();
}
}
global class ContractRenewalBatchJob implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
Date endDateContract = system.today() + Integer.valueOf(Label.KHCPQ_RenewalEmailDuration);
Date endDateContract1 = system.today() + Integer.valueOf(Label.KHCPQ_RenewalEmailDuration1);
Date endDateContract2 = system.today() + Integer.valueOf(Label.KHCPQ_RenewalEmailDuration2);
Date endDateContract3 = system.today() + Integer.valueOf(Label.KHCPQ_RenewalEmailDuration3);
String query = 'select id,Account.Name,Owner.Email,Owner.Name,Ownerid,EndDate,ContractNumber,CustomerSigned.id from Contract where (enddate=:endDateContract OR enddate=:endDateContract1 OR enddate=:endDateContract2 OR enddate=:endDateContract3) and (SBQQ__RenewalForecast__c=false and SBQQ__RenewalQuoted__c=false)';
List<sObject> sobjList = Database.query(query);
system.debug('ttt'+sobjList);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Contract> scope)
{
try
{
List<Messaging.SingleEmailMessage> myEmails = new List<Messaging.SingleEmailMessage>();
for(Contract c:scope)
{
system.debug('Contract : ' + c);
String body;
body = 'Hi ' + c.Owner.Name + ',<br/><br/>';
body = body + 'Contract ' + c.ContractNumber + ' is expiring on ' + c.EndDate.month() + '/' + c.EndDate.day() + '/' + c.EndDate.year() + '.<br/>';
body = body + '<br/>Please renew the Contract ASAP. You can access the contract in the URL below:<br/><br/>';
body = body + Label.WebsiteURL + '/' + c.id + '<br/><br/>';
body = body + 'Regards,<br/>AFH Sales Ops Team';
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setHTMLBody(body);
mail.setToAddresses(new String[]{c.owner.email});
mail.setSubject('ACTION REQUIRED: Your \"' + c.Account.Name + '\" Contract Requires Renewal');
myEmails.add(mail);
}
if(myEmails.size() > 0)
{
Messaging.sendEmail(myEmails,false);
}
}
catch(Exception ex)
{
system.debug('Exception : ' + ex.getMessage());
}
}
global void finish(Database.BatchableContext BC)
{
}
}
test class:
@isTest
public class ContractRenewalBatchJobTest
{
static testMethod void testMethod1()
{
User us = [Select id from User where Id = :UserInfo.getUserId()];
Date startDateWithinRange = system.today();
Date endDateWithinRange = system.today() + 29;
Date endDateWithinRange1 = system.today() + 59;
Date endDateWithinRange2 = system.today() + 89;
Date endDateWithinRange3 = system.today() + 119;
Account acc1 = KHCPQ_Test_DataFactory.createAccount('Parent Account','Active','Street 1','123');
Contract contract1 = KHCPQ_Test_DataFactory.createContract(acc1.Id,'Draft',startDateWithinRange,endDateWithinRange);
Contract contract2 = KHCPQ_Test_DataFactory.createContract(acc1.Id,'Draft',startDateWithinRange,endDateWithinRange1);
Contract contract3 = KHCPQ_Test_DataFactory.createContract(acc1.Id,'Draft',startDateWithinRange,endDateWithinRange2);
Contract contract4 = KHCPQ_Test_DataFactory.createContract(acc1.Id,'Draft',startDateWithinRange,endDateWithinRange3);
System.runAs(us)
Test.startTest();
ContractRenewalBatchJob obj = new ContractRenewalBatchJob();
DataBase.executeBatch(obj);
ContractRenewSchedule sch = new ContractRenewSchedule();
String cronStr = '0 0 0,12 * * ?';
system.schedule('testJob', cronStr, sch);
Test.stopTest();
}
}
Increase code coverage
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines
How to achieve 75 %
https://salesforce.stackexchange.com/questions/24165/why-is-75-code-coverage-required-in-salesforce/24167#24167
https://matheus.dev/write-advanced-test-class-advanced-trigger/
https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test