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
Manasa Dhandapani 7Manasa Dhandapani 7 

how to write test class for the following controller which uses a constructor.

Here is my code. Please help me with the testclass.
public class AccountContactListController { 
   
   
    @AuraEnabled
    public static List<Contact> fetchAccts(id recordId, String pageSize, String pageNumber) {
         List<Contact> conlist = new List<Contact>(); 
        ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(Database.getQueryLocator('SELECT Id, Name, Title, Status__c, Email, Phone, AccountId, Account_Name_Formula__c FROM Contact where Flag_For_Deletion__c = false AND (AccountId =: recordId OR Account.ParentId =: recordId OR Account.Parent.ParentId =: recordId OR Account.Parent.Parent.ParentId =: recordId OR Account.Parent.Parent.Parent.ParentId =: recordId) ORDER BY Status__c ASC, Name ASC'));
         ssc.setpagesize(Integer.valueOf(pageSize));
        ssc.setPageNumber(Integer.valueOf(pageNumber));
         conlist = (List<Contact>)ssc.getRecords();
        return  conlist;  
      }
Thanks in advance.
Daniel AhlDaniel Ahl
Hello there Manasa,
 
@IsTest private static void testFetchAccts(){
        Account acc = new Account(Name = 'Test Account');
        insert acc;
        
        Contact con = new Contact(FirstName = 'John', LastName = 'Doe', Email = 'john.doe@example.com', AccountId = acc.Id);
        insert con;
        AccountContactListController.fetchAccts(acc.Id, '2', '2');
}


Just add more information into the Account that is being created and also the Contact being created. (Such as Statuc__c and other custom fields.)
This class should then cover the most of your fetchAccts method.

Manasa Dhandapani 7Manasa Dhandapani 7
Thank you Daniel. Will try it out