function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC GuestSFDC Guest 

Test class for event object

Hi All,

Please help me to write test class for below class. I have wrote test class but it's not covering.
 
public class eventClass{

// Method to validate start and End Time on events
    public static void ValidateStartEndTime(List<Event> evts) {
        for(Event ev: evts)
        {
            if(ev.StartDateTime.time() < Time.newInstance(7, 30, 0, 0))
            {
                ev.StartDateTime.addError('Start time should be greater than or equals to 7:30 AM');
            }
            if(ev.EndDateTime.time() > Time.newInstance(18, 00, 0, 0))
            {
                ev.EndDateTime.addError('End time should be less than or equals to 6 PM');
            }
        }
    }
}

Test class:
 
@isTest
public class eventClass_Test {

    static testMethod void testMethod1(){
       
        Event e = new Event();
        e.StartDateTime = Datetime.newInstance(2019, 1, 1, 08, 30, 00);
		e.EndDateTime = Datetime.newInstance(2019, 1, 1, 17, 30, 00);
        e.Subject = 'Meeting'; 
        insert e; 
              
        List<Event> eventRecs = new List<Event>();
        eventRecs.add(e);
        eventClass.ValidateStartEndTime(eventRecs);
      
    }

​​​​​​​Thanks
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

You are not using correct values for StartDateTime and EndDateTime in your test class. You need to provide values according to the conditions given in business class. Use below code:
 
@isTest
public class eventClass_Test {

    static testMethod void testMethod1(){
       
        Event e = new Event();
        e.StartDateTime = Datetime.newInstance(2019, 1, 1, 06, 30, 00);
		e.EndDateTime = Datetime.newInstance(2019, 1, 1, 18, 30, 00);
        e.Subject = 'Meeting'; 
        insert e; 
              
        List<Event> eventRecs = new List<Event>();
        eventRecs.add(e);
        eventClass.ValidateStartEndTime(eventRecs);
      
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Andrew GAndrew G
Hi
You could also scare people by adding some asserts.
 
@isTest
public class eventClass_Test {

    static testMethod void testMethod1(){
       
        Event e = new Event();
        e.StartDateTime = Datetime.newInstance(2019, 1, 1, 06, 30, 00);
		e.EndDateTime = Datetime.newInstance(2019, 1, 1, 18, 30, 00);
        e.Subject = 'Meeting'; 
        insert e; 
              
        List<Event> eventRecs = new List<Event>();
        eventRecs.add(e);
        eventClass.ValidateStartEndTime(eventRecs);
       System.assert(error.getMessage().contains('Start time should be greater than or equals to 7:30 AM');
       System.assert(error.getMessage().contains('End time should be less than or equals to 6 PM');

    }
}

Regards
Andrew
Ajay K DubediAjay K Dubedi
Hi,

Test class for event object. This test class is 100% code coverage.
 
@isTest
private class eventClassTest {
    @isTest static void eventClassMethod()
    {
        DateTime myStartDateTime = DateTime.newInstance(2019, 1, 31, 7, 29, 0);
        DateTime myEndDateTime = DateTime.newInstance(2019, 1, 31, 18, 01, 0);
        List<Event> eventList = new List<Event>();
        for(Integer i =0 ; i<1 ; i++)
        {
            Event eventObj = new Event();
            eventObj.StartDateTime = myStartDateTime;
            eventObj.EndDateTime = myEndDateTime;
            eventList.add(eventObj);
        }
        insert eventList;
        Test.startTest();
        eventClass.ValidateStartEndTime(eventList);
        Test.stopTest();
    }
    
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com