Skip to main content Join the Agentforce Virtual Hackathon to build innovative solutions and compete for a $50k Grand Prize. Sign up now. Terms apply.
Hello,

I'm trying to navigate my way through this apex test case and I've not been successful. Could anyone assist with bringing coverage up from 46%?

Thank you.

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){

}

}

 
10 个回答
  1. 2018年12月7日 19:51

    Raj - I was able to get it to work by adding 

    List<Event__c> existingListCompleted = [select id,Completed__c from Event__c where Completed__c = TRUE];

    System.assertequals(existingListCompleted.size(),0);

    between the batch execution and test.stoptest

    Thank you for your help.
  2. 2018年12月7日 19:21
    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. 
  3. 2018年12月7日 18:47
    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();

    }

    }

    WIth the below test class i got 100 % .. can u check is running successfully or now @isTestprivate class CHIEventsMonitorTest { @testSetup static void setup() { Date dt = date.
  4. 2018年12月7日 18:45
    can u share the data model screenshot of the event object .. try ing to do in my org .. 

    Is there any formulas 
  5. 2018年12月7日 17:26
    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. 
  6. 2018年12月7日 16:55
    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){

    }

    }

     
  7. 2018年12月7日 16:51
    Lines 10 - 19 are not covered
  8. 2018年12月7日 16:31
    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){

    }

    }

     
  9. 2018年12月7日 16:21
    Thank you for the fast response Raj. The code coverage is still 46%
  10. 2018年12月7日 16:02
    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();

    }

    }

     
0/9000