Test Class:
@isTest
private class CHIEventsMonitorTest {
@testSetup
static void setup() {
List<Event__c> events = new List<Event__c>();
// insert 10 events
for (Integer i=0;i<10;i++) {
events.add(new Event__c(name='Event '+i,
CreatedDate = DateTime.now(), LastModifiedDate = DateTime.now(), Event_End_Date__c = Date.today(), CHI_Publish_To_Website__c = false, CHI_Status__c = 'Planned', Send_to_Consultants_for_Assignment_c__c = false, EvaluationPilot__c = false, Send_Attendance_Email__c = false, Completed__c = false));
}
insert events;
}
static testmethod void test() {
Test.startTest();
CHIEventsMonitor uca = new CHIEventsMonitor();
Id batchId = Database.executeBatch(uca);
Test.stopTest();
}
}
Note - Most of this was accomplished with the help of Google (obviously, or I probably wouldn't be here)
Batch Class:
global class CHIEventsMonitor implements Database.Batchable<sObject>{
List<Event__c> eventToUpdate = new List<Event__c>();
Date dt = date.today();
String query = 'Select Id, CHI_Status__c, Completed__c, Send_Attendance_Email__c From Event__c WHERE IsFinished__c = FALSE AND Event_End_Date__c =: dt';
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Event__c> event){
for(Event__c c : event){
if(c.Completed__c==FALSE){
c.Send_Attendance_Email__c = TRUE;
c.Completed__c = TRUE;
eventToUpdate .add(c );
}
}
update eventToUpdate ;
}
global void finish(Database.BatchableContext BC){
}
}
Raj - I was able to get it to work by adding
between the batch execution and test.stoptestThank you for your help.List<Event__c> existingListCompleted = [select id,Completed__c from Event__c where Completed__c = TRUE];
System.assertequals(existingListCompleted.size(),0);
Raj - this object is fairly large. I am beginning to wonder if the issue is I'm not putting enough data into the test records - maybe they're failing to create at all? Does that sound like a possibility? I am going to try adding more fields into the list and will report back.Thank you for your help so far. WIth the below test class i got 100 % .. can u check is running successfully or now @isTest
private class CHIEventsMonitorTest {
@testSetup
static void setup() {
Date dt = date.today();
List<Event__c> events = new List<Event__c>();
// insert 10 events
for (Integer i=0;i<10;i++) {
events.add(new Event__c(name='Event '+i,
CHI_Status__c = 'Planned',
Send_Attendance_Email__c = false,
IsFinished__c = false ,
Event_End_Date__c =dt ,
Completed__c = false));
}
insert events;
}
static testmethod void test() {
Test.startTest();
CHIEventsMonitor uca = new CHIEventsMonitor();
Id batchId = Database.executeBatch(uca);
Test.stopTest();
}
}
can u share the data model screenshot of the event object .. try ing to do in my org .. Is there any formulas The query works as is - I have successfully run the batch on a schedule for the last few days. Updating with your change fails. I appreciate your help though Raj.It appears my issue is the test class is not covering the updates to the records. Can u update the apex class as shown above and try to run the test class .. You batch class is not correct i gueess global class CHIEventsMonitor implements Database.Batchable<sObject>{
List<Event__c> eventToUpdate = new List<Event__c>();
Date dt = date.today();
String query = 'Select Id, CHI_Status__c, Completed__c, Send_Attendance_Email__c From Event__c WHERE IsFinished__c = FALSE AND Event_End_Date__c =\''+dt+'\'';
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Event__c> event){
for(Event__c c : event){
if(c.Completed__c==FALSE){
c.Send_Attendance_Email__c = TRUE;
c.Completed__c = TRUE;
eventToUpdate .add(c );
}
}
update eventToUpdate ;
}
global void finish(Database.BatchableContext BC){
}
}
Got it .. You batch apex query is looks not correct and give me whihc lines are not covering Change your batch apex class as below global class CHIEventsMonitor implements Database.Batchable<sObject>{
List<Event__c> eventToUpdate = new List<Event__c>();
Date dt = date.today();
String query = 'Select Id, CHI_Status__c, Completed__c, Send_Attendance_Email__c From Event__c WHERE IsFinished__c = FALSE AND Event_End_Date__c =\''+dt+'\'';
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Event__c> event){
for(Event__c c : event){
if(c.Completed__c==FALSE){
c.Send_Attendance_Email__c = TRUE;
c.Completed__c = TRUE;
eventToUpdate .add(c );
}
}
update eventToUpdate ;
}
global void finish(Database.BatchableContext BC){
}
}
try this @isTest
private class CHIEventsMonitorTest {
@testSetup
static void setup() {
Date dt = date.today();
List<Event__c> events = new List<Event__c>();
// insert 10 events
for (Integer i=0;i<10;i++) {
events.add(new Event__c(name='Event '+i,
CreatedDate = DateTime.now(), LastModifiedDate = DateTime.now(),
Event_End_Date__c = Date.today(),
CHI_Publish_To_Website__c = false,
CHI_Status__c = 'Planned',
Send_to_Consultants_for_Assignment_c__c = false,
EvaluationPilot__c = false,
Send_Attendance_Email__c = false,
IsFinished__c = false ,
Event_End_Date__c =dt ,
Completed__c = false));
}
insert events;
}
static testmethod void test() {
Test.startTest();
CHIEventsMonitor uca = new CHIEventsMonitor();
Id batchId = Database.executeBatch(uca);
Test.stopTest();
}
}