Test class for Task Comp. apex - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Jonathan WolffJonathan Wolff 

Test class for Task Comp. apex

Hello,
I build a custom Event component that I would like to convert into Liveground. I know, that I need to build a test class for my apex class.
The component just displays the subject, activityDate, Start and EndDate of an event. Could you give me a code for the test class.
The apex class code is this:

public with sharing class EventController {
  

    @AuraEnabled
  public static List<Event> loadEvents(Id recordId){
      string userId = UserInfo.getUserId();
    return[SELECT Subject, ActivityDate, StartDateTime, EndDateTime FROM Event WHERE WhatId=:recordId And ActivityDate>= TODAY 
           AND ActivityDate  < Next_N_Days:1 AND OwnerId=:userId ];
  }
    @AuraEnabled
  public static List<Event> loadEvents2(Id recordId){
      string userId = UserInfo.getUserId();
      Date tomorrowDate = Date.today().addDays(1);
    Date dayAfterTomorrow = tomorrowDate.addDays(1);
    return[SELECT Subject, ActivityDate, StartDateTime, EndDateTime FROM Event WHERE WhatId=:recordId And ActivityDate>= :tomorrowDate AND ActivityDate  < :dayAfterTomorrow AND OwnerId=:userId ];
  }

    @AuraEnabled
  public static List<Event> loadEvents3(Id recordId){
      string userId = UserInfo.getUserId();
      Date tomorrowDate = Date.today().addDays(1);
      Date dayAfterTomorrow = tomorrowDate.addDays(1);
      Date twodaysAfterTomorrow = tomorrowDate.addDays(2);
    return[SELECT Subject, ActivityDate, StartDateTime, EndDateTime FROM Event WHERE WhatId=:recordId And ActivityDate>= :dayAfterTomorrow AND ActivityDate  < :twodaysAfterTomorrow AND OwnerId=:userId ];
  }
    
}
 
Best Answer chosen by Jonathan Wolff
Usman AliUsman Ali
Sorry I was using wrong methods name.Try below code.
@isTest
private class EventControllerTest 
{
    static testMethod void testMethod1() 
    {
        Account ac = new Account(Name = 'Test Account');
        insert ac ;

        Event evnt = new Event(Subject = 'Call', StartDateTime = Datetime.now().addDays(-1), Description = 'Test', WhatId = ac.Id );
        insert evnt ;

        EventController.loadEvents(ac.Id);

        evnt.StartDateTime = Datetime.now().addDays(1);
        update evnt;
        EventController.loadEvents2(ac.Id);


        evnt.StartDateTime = Datetime.now().addDays(2);
        update evnt;
        EventController.loadEvents3(ac.Id);


        evnt.StartDateTime = Datetime.now().addDays(3);
        update evnt;
        EventController.loadEvents4(ac.Id);

    }
}

 

All Answers

Usman AliUsman Ali
Hi Jonathan,

Here you go...
@isTest
private class EventControllerTest 
{
    static testMethod void testMethod1() 
    {
        Account ac = new Account(Name = 'Test Account');
        insert ac ;

        Event evnt = new Event(Subject = 'Call', StartDateTime = Datetime.now().addDays(-1), Description = 'Test', WhatId = ac.Id );
        insert evnt ;

        EventController.loadTasks(ac.Id);

        evnt.StartDateTime = Datetime.now().addDays(1);
        update evnt;
        EventController.loadTasks2(ac.Id);


        evnt.StartDateTime = Datetime.now().addDays(2);
        update evnt;
        EventController.loadTasks3(ac.Id);


        evnt.StartDateTime = Datetime.now().addDays(3);
        update evnt;
        EventController.loadTasks4(ac.Id);

    }
}

 
Jonathan WolffJonathan Wolff
Hi, I inserted the code and I have 4 errors. Could you tell me what I did wrong and how to fix it?
Greetings
User-added image
Usman AliUsman Ali
Sorry I was using wrong methods name.Try below code.
@isTest
private class EventControllerTest 
{
    static testMethod void testMethod1() 
    {
        Account ac = new Account(Name = 'Test Account');
        insert ac ;

        Event evnt = new Event(Subject = 'Call', StartDateTime = Datetime.now().addDays(-1), Description = 'Test', WhatId = ac.Id );
        insert evnt ;

        EventController.loadEvents(ac.Id);

        evnt.StartDateTime = Datetime.now().addDays(1);
        update evnt;
        EventController.loadEvents2(ac.Id);


        evnt.StartDateTime = Datetime.now().addDays(2);
        update evnt;
        EventController.loadEvents3(ac.Id);


        evnt.StartDateTime = Datetime.now().addDays(3);
        update evnt;
        EventController.loadEvents4(ac.Id);

    }
}

 
This was selected as the best answer
Jonathan WolffJonathan Wolff
Okay so can I delete the last part because this one doesn't have loadEvents4?
Thank you for your help.
Usman AliUsman Ali
Yes you can delete last 3 lines after LoadEvents3