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
Alan FloresAlan Flores 

Test Class for custom controller with a query

Hi everyone! I hope you can help me with this:

I was requested to create test class for some custom controllers I made, the problems is that I don't understand very well the test class examples I found here in trailhead and another blogs, since they are very specific examples and none of those use queries. So, my code is this:

 

public class AccountListController{

    public AccountListController(ApexPages.StandardController controller) {

    }

//It creates a list from Session__c called "accList"
    public list<Session__c> accList{get;set;}
    public AccountListController (){

//This query fills the list "accList" with the requested files from Session__c
        accList = [select Training__c, Date__c, Start_Time__c, End_Time__c, Training__r.Name, Name, id from Session__c];
    }

}

Thanks in advance for your help :)
Akhilesh Reddy BaddigamAkhilesh Reddy Baddigam
You can refer testing in Controller classes as in this link (https://developer.salesforce.com/forums/?id=906F00000008xY2IAI). coming to soql query as long as you instantiate the controller classs by creating test data (https://trailhead.salesforce.com/en/modules/apex_testing/units/apex_testing_data) by inserting session objects with the fields in the SOQL query, it should give you the coverage you needed. 

 
@isTest
public class AccountListControllerTest{

  @isTest
  public static void testSesion(){
  Session__c s=new Session__c();

  s.Training__c='';
  //remaining fields 

  insert s;

  AccountListController a=new AccountListController();

}

}

 
Alan FloresAlan Flores
Thank you Akhilesh Reddy Baddigam, for the tip to correctly post my question and the code. The code run correctly except for the part of the insert, I just have to change it to update and worked fine! Thanks a lot.