How to test apex in salesforce developer console - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Jonathan WolffJonathan Wolff 

How to test apex in salesforce developer console

Hi I build a Class in Apex for my component and just build a test class. I tried to click on run at the test class and all I got is
:User-added image
Here is my apex:


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 ];
  }
    
}



And my Test class I build has the following code sample:

@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);
       
    }
}
Best Answer chosen by Jonathan Wolff
Ankush AgarwalAnkush Agarwal
Hi Jonathan,

You may click on EventControllerTest at the bottom in Tests tab. You will get first method and when you click on that method you will able to see the error detailsUser-added image

You may try with below updated test class:
 
@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), DurationInMinutes = 10, 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);
       
    }
}

 

All Answers

Usman AliUsman Ali
Hi Jonathan,

Go to Setup | Apex Test Execution then click on Select Tests and Select EventControllerTest after executing test you can see result at the bottom of page when you click on EventControllerTest in that window.

Or open EventControllerTest in developer console and press Run Test button from top right side. 

User-added image
 
Amnon KruviAmnon Kruvi
Hi Jonathan,

If you open the folder with the test results and double-click on any of the items there, you should see which of your test methods failed - and more importantly - why they failed. 
Ankush AgarwalAnkush Agarwal
Hi Jonathan,

You may click on EventControllerTest at the bottom in Tests tab. You will get first method and when you click on that method you will able to see the error detailsUser-added image

You may try with below updated test class:
 
@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), DurationInMinutes = 10, 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);
       
    }
}

 
This was selected as the best answer