You need to sign in to do that
Don't have an account?
Hughbert Strong
Help with Code Coverage on Scheduled Apex Class
Hello friends,
I'm fairly new to writting Apex code, and I'm running code covearge issues with my scheduled apex class. 50% 4/8 lines. Can someone help me out?
Apex Class:
global class scheduleExpiredEntitlements implements Schedulable{
global static void scheduleMe(){
scheduleExpiredEntitlements msc = new scheduleExpiredEntitlements();
String sch= '0 00 00 * * ?';
String jobID = System.schedule('Scheduled Job', sch, msc);
}
global void execute(SchedulableContext sc){
List<Entitlement> EntIds = [Select Id, Name, EndDate, CasesPerEntitlement, RemainingCases, IsPerIncident, AccountId from Entitlement Where EndDate = Yesterday];
for(integer i = 0; i< EntIds.size(); i++){
}
update EntIds;
}
}
Test Class:
@istest
public class TestScheduleExpiredEntitlements {
@istest static void testEntitlements(){
Date Ender = Date.today()-1;
Date Starter = Date.today()-365;
Account a = New Account();
a.Name = 'Hieu Truong';
insert a;
Entitlement e = New Entitlement ();
e.Name ='Test Entitlement';
e.StartDate = Starter;
e.EndDate = Ender;
e.CasesPerEntitlement = 99;
e.RemainingCases = 99;
e.IsPerIncident = true;
e.AccountId = a.Id;
insert e;
String CRON_EXP = '0 0 0 15 3 ? *';
String jobId = System.schedule('ScheduleApexClassTest', CRON_EXP, new scheduleExpiredEntitlements());
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
System.assertEquals(CRON_EXP, ct.CronExpression);
System.assertEquals(0, ct.TimesTriggered);
}
}
I'm fairly new to writting Apex code, and I'm running code covearge issues with my scheduled apex class. 50% 4/8 lines. Can someone help me out?
Apex Class:
global class scheduleExpiredEntitlements implements Schedulable{
global static void scheduleMe(){
scheduleExpiredEntitlements msc = new scheduleExpiredEntitlements();
String sch= '0 00 00 * * ?';
String jobID = System.schedule('Scheduled Job', sch, msc);
}
global void execute(SchedulableContext sc){
List<Entitlement> EntIds = [Select Id, Name, EndDate, CasesPerEntitlement, RemainingCases, IsPerIncident, AccountId from Entitlement Where EndDate = Yesterday];
for(integer i = 0; i< EntIds.size(); i++){
}
update EntIds;
}
}
Test Class:
@istest
public class TestScheduleExpiredEntitlements {
@istest static void testEntitlements(){
Date Ender = Date.today()-1;
Date Starter = Date.today()-365;
Account a = New Account();
a.Name = 'Hieu Truong';
insert a;
Entitlement e = New Entitlement ();
e.Name ='Test Entitlement';
e.StartDate = Starter;
e.EndDate = Ender;
e.CasesPerEntitlement = 99;
e.RemainingCases = 99;
e.IsPerIncident = true;
e.AccountId = a.Id;
insert e;
String CRON_EXP = '0 0 0 15 3 ? *';
String jobId = System.schedule('ScheduleApexClassTest', CRON_EXP, new scheduleExpiredEntitlements());
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
System.assertEquals(CRON_EXP, ct.CronExpression);
System.assertEquals(0, ct.TimesTriggered);
}
}
Apex Class:
global class scheduleExpiredEntitlements implements Schedulable{
global void execute(SchedulableContext sc){
List<Entitlement> ExpiredIds = [Select Id from Entitlement Where EndDate = Yesterday];
List<Entitlement> WillExpireIds = [Select Id from Entitlement Where EndDate = LAST_N_DAYS:90];
for(integer i = 0; i< ExpiredIds.size(); i++){
}
for(integer i = 0; i< WillExpireIds.size(); i++){
}
update ExpiredIds;
update WillExpireIds;
}
}
Test Code:
@istest
public class TestScheduleExpiredEntitlements {
public static testmethod void test1(){
Test.StartTest();
Date Ender = Date.today()-1;
Date Starter = Date.today()-365;
Account a = New Account();
a.Name = 'Hieu Truong';
insert a;
for (integer i = 0; i< 10; i++){
Entitlement e = New Entitlement ();
e.Name ='Test Entitlement';
e.StartDate = Starter;
e.EndDate = Ender;
e.CasesPerEntitlement = 99;
e.RemainingCases = 99;
e.IsPerIncident = true;
e.AccountId = a.Id;
insert e;
}
String CRON_EXP = '0 0 0 3 9 ? 2022';
String jobId = System.schedule('TestScheduleExpiredEntitlements', CRON_EXP, new scheduleExpiredEntitlements());
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
System.assertEquals(CRON_EXP, ct.CronExpression);
System.assertEquals(0, ct.TimesTriggered);
Test.stopTest();
}
}
All Answers
Hey Hughbert...
Do not forget to put Test.startTest(); before
String jobId = System.schedule('ScheduleApexClassTest', CRON_EXP, new scheduleExpiredEntitlements());
and Test.stopTest(); after it.
This way, you make sure the scheduler will execute.
Apex Class:
global class scheduleExpiredEntitlements implements Schedulable{
global void execute(SchedulableContext sc){
List<Entitlement> ExpiredIds = [Select Id from Entitlement Where EndDate = Yesterday];
List<Entitlement> WillExpireIds = [Select Id from Entitlement Where EndDate = LAST_N_DAYS:90];
for(integer i = 0; i< ExpiredIds.size(); i++){
}
for(integer i = 0; i< WillExpireIds.size(); i++){
}
update ExpiredIds;
update WillExpireIds;
}
}
Test Code:
@istest
public class TestScheduleExpiredEntitlements {
public static testmethod void test1(){
Test.StartTest();
Date Ender = Date.today()-1;
Date Starter = Date.today()-365;
Account a = New Account();
a.Name = 'Hieu Truong';
insert a;
for (integer i = 0; i< 10; i++){
Entitlement e = New Entitlement ();
e.Name ='Test Entitlement';
e.StartDate = Starter;
e.EndDate = Ender;
e.CasesPerEntitlement = 99;
e.RemainingCases = 99;
e.IsPerIncident = true;
e.AccountId = a.Id;
insert e;
}
String CRON_EXP = '0 0 0 3 9 ? 2022';
String jobId = System.schedule('TestScheduleExpiredEntitlements', CRON_EXP, new scheduleExpiredEntitlements());
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
System.assertEquals(CRON_EXP, ct.CronExpression);
System.assertEquals(0, ct.TimesTriggered);
Test.stopTest();
}
}