+ Start a Discussion
RichardR1RichardR1 

Code coverage on Event controller test class

Hi guys, for some reason my test class says Pass in the Developer Console but code coverage is 0% at the same time. How can it pass the test if there is 0%? I only used a test class generator because I'm not good at creating test classess yet.

Here is my custom controller:
public class ContactTasks{
    public Event event;
    public ContactTasks() {
        event = [SELECT FIELDS(STANDARD) FROM Event WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }
    public Event getEvent(){
        return event;
    }
    List <Task> relatedTasks;
        public List<Task> getrelatedTasks(){
        relatedTasks = [SELECT FIELDS(STANDARD)
                FROM Task
                WHERE WhoId= :this.event.WhoId
                ORDER BY ActivityDate DESC];
        return relatedTasks;
    }

    
}
Here is my test class:
@isTest
private class ContactTasks_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    Contact contact_Obj = new Contact(LastName='Test');
    Insert contact_Obj;
    Event eventobj = new Event(WhoId = contact_Obj.id, DurationInMinutes = 10, ActivityDateTime = Datetime.now());
    Insert eventobj;
    Task task_Obj = new Task(WhoId = contact_Obj.id, ActivityDate = Date.today(), Status = 'Not Started', Priority = 'High', Description = '12', IsReminderSet = false, IsRecurrence = false, Placeholder_for_next_call_set__c = false, BD_Meeting_scheduled_by_InMail__c = false, Duplicate_Meeting__c = false, Automatic_Reply__c = false, Candidate_InMail__c = false, Bounced__c = false);
    Insert task_Obj; 
    test.stopTest();
  }
  static testMethod void test_getEvent_UseCase1(){
    List<Task> task_Obj  =  [SELECT Id,WhoId,WhatId,WhoCount,WhatCount,Subject,ActivityDate,Status,Priority,Description,IsRecurrence,Placeholder_for_next_call_set__c from Task];
    System.assertEquals(true,task_Obj.size()>0);
    PageReference pageRef = Page.ContactsTasks;
    pageRef.getParameters().put('id','test');
    Test.setCurrentPage(pageRef);
    Contact contact_Obj = new Contact(LastName='Test');
    Insert contact_Obj;
    Event eventobj = new Event(WhoId = contact_Obj.id, DurationInMinutes = 10, ActivityDateTime = Datetime.now());
    Insert eventobj;

  }

    static testMethod void test_getEvent_UseCase3(){
    List<Task> task_Obj  =  [SELECT Id,WhoId,WhatId,WhoCount,WhatCount,Subject,ActivityDate,Status,Priority,Description,IsRecurrence,Placeholder_for_next_call_set__c from Task];
    System.assertEquals(true,task_Obj.size()>0);
    PageReference pageRef = Page.ContactsTasks;
    pageRef.getParameters().put('id','test');
    Test.setCurrentPage(pageRef);


    task_Obj[0].ActivityDate = date.parse('7/13/2022');
    task_Obj[0].Status='Not Started';
    task_Obj[0].Priority='High';
    task_Obj[0].Description = '0';

  }
  static testMethod void test_getrelatedTasks_UseCase1(){
    List<Task> task_Obj  =  [SELECT Id,WhoId,WhatId,WhoCount,WhatCount,Subject,ActivityDate,Status,Priority,Description,IsRecurrence,Placeholder_for_next_call_set__c from Task];
    System.assertEquals(true,task_Obj.size()>0);
    PageReference pageRef = Page.ContactsTasks;
    pageRef.getParameters().put('id','test');
    Test.setCurrentPage(pageRef);

  }

    static testMethod void test_getrelatedTasks_UseCase2(){
    List<Task> task_Obj  =  [SELECT Id,WhoId,WhatId,WhoCount,WhatCount,Subject,ActivityDate,Status,Priority,Description,IsRecurrence,Placeholder_for_next_call_set__c from Task];
    System.assertEquals(true,task_Obj.size()>0);
    PageReference pageRef = Page.ContactsTasks;
    pageRef.getParameters().put('id','test');
    Test.setCurrentPage(pageRef);


    task_Obj[0].ActivityDate = date.parse('7/13/2022');
    task_Obj[0].Status='Not Started';
    task_Obj[0].Priority='High';
    task_Obj[0].Description = '0';

  }
}
Best Answer chosen by RichardR1
AnkaiahAnkaiah (Salesforce Developers) 
Hi Richard,

You need to call the apex class in the test class.
@isTest
private class ContactTasks_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    Contact contact_Obj = new Contact(LastName='Test');
    Insert contact_Obj;
    Event eventobj = new Event(WhoId = contact_Obj.id, DurationInMinutes = 10, ActivityDateTime = Datetime.now());
    Insert eventobj;
    Task task_Obj = new Task(WhoId = contact_Obj.id, ActivityDate = Date.today(), Status = 'Not Started', Priority = 'High', Description = '12', IsReminderSet = false, IsRecurrence = false, Placeholder_for_next_call_set__c = false, BD_Meeting_scheduled_by_InMail__c = false, Duplicate_Meeting__c = false, Automatic_Reply__c = false, Candidate_InMail__c = false, Bounced__c = false);
    Insert task_Obj; 
    test.stopTest();
  }
  static testMethod void test_getEvent_UseCase1(){
    List<Task> task_Obj  =  [SELECT Id,WhoId,WhatId,WhoCount,WhatCount,Subject,ActivityDate,Status,Priority,Description,IsRecurrence,Placeholder_for_next_call_set__c from Task];
    System.assertEquals(true,task_Obj.size()>0);
    PageReference pageRef = Page.ContactsTasks;
    pageRef.getParameters().put('id','test');
    Test.setCurrentPage(pageRef);
    Contact contact_Obj = new Contact(LastName='Test');
    Insert contact_Obj;
    Event eventobj = new Event(WhoId = contact_Obj.id, DurationInMinutes = 10, ActivityDateTime = Datetime.now());
    Insert eventobj;
ContactTasks ct = new ContactTasks();
ct.ContactTasks();
ct.getEvent();
ct.getrelatedTasks();

  }

    static testMethod void test_getEvent_UseCase3(){
    List<Task> task_Obj  =  [SELECT Id,WhoId,WhatId,WhoCount,WhatCount,Subject,ActivityDate,Status,Priority,Description,IsRecurrence,Placeholder_for_next_call_set__c from Task];
    System.assertEquals(true,task_Obj.size()>0);
    PageReference pageRef = Page.ContactsTasks;
    pageRef.getParameters().put('id','test');
    Test.setCurrentPage(pageRef);


    task_Obj[0].ActivityDate = date.parse('7/13/2022');
    task_Obj[0].Status='Not Started';
    task_Obj[0].Priority='High';
    task_Obj[0].Description = '0';
ContactTasks ct = new ContactTasks();
ct.ContactTasks();
ct.getEvent();
ct.getrelatedTasks();

  }
  static testMethod void test_getrelatedTasks_UseCase1(){
    List<Task> task_Obj  =  [SELECT Id,WhoId,WhatId,WhoCount,WhatCount,Subject,ActivityDate,Status,Priority,Description,IsRecurrence,Placeholder_for_next_call_set__c from Task];
    System.assertEquals(true,task_Obj.size()>0);
    PageReference pageRef = Page.ContactsTasks;
    pageRef.getParameters().put('id','test');
    Test.setCurrentPage(pageRef);
ContactTasks ct = new ContactTasks();
ct.ContactTasks();
ct.getEvent();
ct.getrelatedTasks();

  }

    static testMethod void test_getrelatedTasks_UseCase2(){
    List<Task> task_Obj  =  [SELECT Id,WhoId,WhatId,WhoCount,WhatCount,Subject,ActivityDate,Status,Priority,Description,IsRecurrence,Placeholder_for_next_call_set__c from Task];
    System.assertEquals(true,task_Obj.size()>0);
    PageReference pageRef = Page.ContactsTasks;
    pageRef.getParameters().put('id','test');
    Test.setCurrentPage(pageRef);


    task_Obj[0].ActivityDate = date.parse('7/13/2022');
    task_Obj[0].Status='Not Started';
    task_Obj[0].Priority='High';
    task_Obj[0].Description = '0';
ContactTasks ct = new ContactTasks();
ct.ContactTasks();
ct.getEvent();
ct.getrelatedTasks();


  }
}

If this helps, Please mark it as best answer,

Thanks!!

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Richard,

You need to call the apex class in the test class.
@isTest
private class ContactTasks_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    Contact contact_Obj = new Contact(LastName='Test');
    Insert contact_Obj;
    Event eventobj = new Event(WhoId = contact_Obj.id, DurationInMinutes = 10, ActivityDateTime = Datetime.now());
    Insert eventobj;
    Task task_Obj = new Task(WhoId = contact_Obj.id, ActivityDate = Date.today(), Status = 'Not Started', Priority = 'High', Description = '12', IsReminderSet = false, IsRecurrence = false, Placeholder_for_next_call_set__c = false, BD_Meeting_scheduled_by_InMail__c = false, Duplicate_Meeting__c = false, Automatic_Reply__c = false, Candidate_InMail__c = false, Bounced__c = false);
    Insert task_Obj; 
    test.stopTest();
  }
  static testMethod void test_getEvent_UseCase1(){
    List<Task> task_Obj  =  [SELECT Id,WhoId,WhatId,WhoCount,WhatCount,Subject,ActivityDate,Status,Priority,Description,IsRecurrence,Placeholder_for_next_call_set__c from Task];
    System.assertEquals(true,task_Obj.size()>0);
    PageReference pageRef = Page.ContactsTasks;
    pageRef.getParameters().put('id','test');
    Test.setCurrentPage(pageRef);
    Contact contact_Obj = new Contact(LastName='Test');
    Insert contact_Obj;
    Event eventobj = new Event(WhoId = contact_Obj.id, DurationInMinutes = 10, ActivityDateTime = Datetime.now());
    Insert eventobj;
ContactTasks ct = new ContactTasks();
ct.ContactTasks();
ct.getEvent();
ct.getrelatedTasks();

  }

    static testMethod void test_getEvent_UseCase3(){
    List<Task> task_Obj  =  [SELECT Id,WhoId,WhatId,WhoCount,WhatCount,Subject,ActivityDate,Status,Priority,Description,IsRecurrence,Placeholder_for_next_call_set__c from Task];
    System.assertEquals(true,task_Obj.size()>0);
    PageReference pageRef = Page.ContactsTasks;
    pageRef.getParameters().put('id','test');
    Test.setCurrentPage(pageRef);


    task_Obj[0].ActivityDate = date.parse('7/13/2022');
    task_Obj[0].Status='Not Started';
    task_Obj[0].Priority='High';
    task_Obj[0].Description = '0';
ContactTasks ct = new ContactTasks();
ct.ContactTasks();
ct.getEvent();
ct.getrelatedTasks();

  }
  static testMethod void test_getrelatedTasks_UseCase1(){
    List<Task> task_Obj  =  [SELECT Id,WhoId,WhatId,WhoCount,WhatCount,Subject,ActivityDate,Status,Priority,Description,IsRecurrence,Placeholder_for_next_call_set__c from Task];
    System.assertEquals(true,task_Obj.size()>0);
    PageReference pageRef = Page.ContactsTasks;
    pageRef.getParameters().put('id','test');
    Test.setCurrentPage(pageRef);
ContactTasks ct = new ContactTasks();
ct.ContactTasks();
ct.getEvent();
ct.getrelatedTasks();

  }

    static testMethod void test_getrelatedTasks_UseCase2(){
    List<Task> task_Obj  =  [SELECT Id,WhoId,WhatId,WhoCount,WhatCount,Subject,ActivityDate,Status,Priority,Description,IsRecurrence,Placeholder_for_next_call_set__c from Task];
    System.assertEquals(true,task_Obj.size()>0);
    PageReference pageRef = Page.ContactsTasks;
    pageRef.getParameters().put('id','test');
    Test.setCurrentPage(pageRef);


    task_Obj[0].ActivityDate = date.parse('7/13/2022');
    task_Obj[0].Status='Not Started';
    task_Obj[0].Priority='High';
    task_Obj[0].Description = '0';
ContactTasks ct = new ContactTasks();
ct.ContactTasks();
ct.getEvent();
ct.getrelatedTasks();


  }
}

If this helps, Please mark it as best answer,

Thanks!!
This was selected as the best answer
RichardR1RichardR1
Thanks Ankaiah but now I get this error from the dev console: 
System.QueryException: List has no rows for assignment to SObject
Class.ContactTasks.<init>: line 4, column 1
Class.ContactTasks_Test3.getEvent: line 16, column 1
Class.ContactTasks.<init>: line 4, column 1
Class.ContactTasks_Test3.getrelatedTasks: line 31, column 1
User-added image
ravi soniravi soni
Hi Richard,
Try below test class. I am not able to save this class because I don't have ContactsTasks Vf page but I have written test class. It might help you.
@isTest
public class ContactTasksTest {
    @isTest
    public static void test_unit(){
        test.startTest();
        Contact con = new Contact(LastName='TestData');
        insert con;
        Event oEvent = new Event(WhoId = con.id, DurationInMinutes = 10, ActivityDateTime = Datetime.now());
        insert oEvent;
        Task oTask = new Task(WhoId = con.id, ActivityDate = Date.today(), Status = 'Not Started', Priority = 'High', 
                              Description = '12', IsReminderSet = false, IsRecurrence = false);
       insert oTask; 
        /* Placeholder_for_next_call_set__c = false, BD_Meeting_scheduled_by_InMail__c = false, 
Duplicate_Meeting__c = false, Automatic_Reply__c = false, Candidate_InMail__c = false, 
Bounced__c = false*/
         
        PageReference pageRef = Page.ContactsTasks;
        pageRef.getParameters().put('id',oEvent.Id);
        Test.setCurrentPage(pageRef);
        ContactTasks oContactTasks = new ContactTasks();
        oContactTasks.getEvent();
        oContactTasks.getrelatedTasks();
        
        test.stopTest();
    }   
    
}

let me know by marking it as the best mark.
Thank you​​​​​​​
AnkaiahAnkaiah (Salesforce Developers) 
Hi Richard,

Replace the  pageRef.getParameters().put('id','test');  in your test class with below code.

pageRef.getParameters().put('id',eventobj.id); 

Please let me know if any issues.

Thanks!!