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
Venkateswarlu PVenkateswarlu P 

Not getting 100% code coverage for Event Test Class

public class Contact_Trigger_Handler {
	//whenever a new contact is created, create an event for it.
    public static void eventOnContact(List<contact> conList) {
        List<Event> eventList = new List<Event>();
        For(Contact c : conList){
            Event e = new Event();
            e.Subject=c.LastName;
            e.WhatId = c.accountId;
            e.whoId = c.Id;
            e.Type = 'Contact';
            e.IsAllDayEvent = true;                 	
        	e.StartDateTime = DateTime.now();
            e.EndDateTime=DateTime.now();
            eventList.add(e);            
        }
        insert eventList;        
    }
Test Class
-------------------------------------------------------
@isTest
    static void event(){
        Contact c = new Contact();
        c.lastName='Pawan';
        insert c;   
        Event e = new Event();
            e.Subject=c.lastName;
            e.WhatId = c.accountId;
            e.whoId = c.Id;
            e.Type = 'Contact';
            e.IsAllDayEvent = true;                 	
            e.StartDateTime = DateTime.now();
            e.EndDateTime=DateTime.now();           
        insert e;
        Integer count = [select count() from event limit 1];
        system.assertEquals(count, 1);
        system.assertEquals(e.Subject, c.lastName);
    }

 
Best Answer chosen by Venkateswarlu P
Sampath SuranjiSampath Suranji
Hi,
Please try below.
@isTest
public class Contact_Trigger_HandlerTest {
    
  public  static testMethod void  event(){
      List<Contact> contactList = new LIST<contact>();
        Contact c = new Contact();
        c.lastName='Pawan';
        insert c;  
        contactList.add(c);
        Contact_Trigger_Handler.eventOnContact(contactList);
        Integer count = [select count() from event limit 1];
        system.assertEquals(count, 1);
        //system.assertEquals(e.Subject, c.lastName);
    }

}

Best Regards

All Answers

Sampath SuranjiSampath Suranji
Hi,
Please try below.
@isTest
public class Contact_Trigger_HandlerTest {
    
  public  static testMethod void  event(){
      List<Contact> contactList = new LIST<contact>();
        Contact c = new Contact();
        c.lastName='Pawan';
        insert c;  
        contactList.add(c);
        Contact_Trigger_Handler.eventOnContact(contactList);
        Integer count = [select count() from event limit 1];
        system.assertEquals(count, 1);
        //system.assertEquals(e.Subject, c.lastName);
    }

}

Best Regards
This was selected as the best answer
Venkateswarlu PVenkateswarlu P
The code coverage is still 74% only 
Sampath SuranjiSampath Suranji
Hi,
I tested and it is given 100% for me. Do you have any other methods inside the Contact_Trigger_Handler  class?
Regards
Venkateswarlu PVenkateswarlu P
Yes.Due to other methods we are not getting 100% coverage. There is no issue with both test classes.
Thanks