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
Nishit MistryyyNishit Mistryyy 

Writing a test class for the apex class that is fetching data from the salesforce org.

Hello,
I have a apex class defined as ContactListController, this class basically gets the list of contacts from my salesforce developer org. I want to write the test class for it.
The code is: 
public class ContactListController {
        
    @AuraEnabled
    public static List<Contact> getContactList(List<Id> accountIds) {
        // Getting the list of contacts from where Id is in accountIds
                List<Contact> contactList = [SELECT Id, Name, Email FROM Contact WHERE AccountId in :accountIds];
                // Returning the contact list
        return contactList;
    }
}
Best Answer chosen by Nishit Mistryyy
Sai PraveenSai Praveen (Salesforce Developers) 
HI Nishit,

Can you try the below test class. it should give you 100% coverage.

While inserting Account and Contact make sure all the required fields were defined below.
 
@istest
public class ContactListControllerTest {
 static testmethod void contrallerTest(){
     
account acc = new account();
        acc.name='test';
         insert acc;
     Contact con= new Contact();
     con.LastName='sample';
     con.AccountId=acc.id;
     insert Con;
     List<Id> accids= new List<Id>();
     accids.add(acc.id);
     List<Contact> conlist=ContactListController.getContactList(accids);
     system.assertEquals(1, conlist.size());
}
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,