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
Giussep EstradaGiussep Estrada 

Help with my First Test Class

Hello guys
First post
I'm a newbie in Salesforce trying to understand how i can write test classes
I need to write a test class of the following class:
public class RelatedListEditController {
    @AuraEnabled( cacheable=true )  
    public static List < sObject > fetchRecords( String listValues ) {  
        List < String > strList = listValues.split( ',' );  
        if ( strList.size() >= 3 ) {  
            String recordId = strList.get( 0 );  
            String objectName = strList.get( 1 );  
            String parentFieldAPIName = strList.get( 2 );              
            String strSOQL = 'SELECT Id FROM ' + objectName + ' WHERE ' + parentFieldAPIName + ' = \'' + recordId + '\'' + ' WITH ' + 'SECURITY_ENFORCED';  
            strSOQL += ' LIMIT 20';
            return Database.query( strSOQL );  
        } else   
            return null;  
    }   
}
Any help would be appreciated
I just want to learn how i can do it
Best Answer chosen by Giussep Estrada
Derrick AbbeyDerrick Abbey
Hi Giussep,

I would suggest you check out the section on writing unit tests in the Apex developer guide. 
Apex Developer Guide (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing.htm)

For the code you published, here is a sample test class to get you started.  I hope this helps.
@isTest //denotes that this is a test class
//I like to use a naming convention for my test classes.  I usually use the name of the class that I'm testing,
//followed by '_Test'
public class RelatedListEditController_Test {
    
    //setup method to create test data
    @TestSetup static void createTestData(){
        //insert whatever data that you want available for your tests here.
        //Example
        //create and insert test accounts
        Account testAccount = new Account(Name = 'Test Account 1');

        insert testAccount;

        //create and insert test contacts
        List<Contact> testContacts = new List<Contact>{
            new Contact(LastName = 'Test 1', AccountId = testAccount.Id),
            new Contact(LastName = 'Test 2', AccountId = testAccount.Id)
        };
        insert testContacts;

    }//end of testSetup

    //test the method for the records we created in the test setup
    @isTest static void testFetchRecords(){
        //get one of the test accounts
        Account testAccount = [SELECT Id FROM Account LIMIT 1];

        //create string of values to pass into method
        String values = testAccount.Id + ',Contact,AccountId';

        //execute the method
        List<Sobject> returnedList = RelatedListEditController.fetchRecords(values);

        //check our results
        System.assertEquals(2, returnedList.size());

        //check the object types to make sure they match
        for(SObject so: returnedList){
            System.assertEquals('Contact', so.getSObjectType().getDescribe().getName());
        }

        @isTest static void testEmptyResults(){
            //we would want a test here to test a scenario where we passed in correct values, but the query returned zero results
        }

        @isTest static void testNullResults(){
            //here we would pass in a parameter string without enough arguments and make sure we get a null returned.
        }

    }
}

 

All Answers

Agustin BAgustin B
Hi Glussep, read this official link from salesforce that will help you write your class:
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/apps_packaging_tests.htm

If it helps please like and mark as correct, it may help others with the same issue
Derrick AbbeyDerrick Abbey
Hi Giussep,

I would suggest you check out the section on writing unit tests in the Apex developer guide. 
Apex Developer Guide (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing.htm)

For the code you published, here is a sample test class to get you started.  I hope this helps.
@isTest //denotes that this is a test class
//I like to use a naming convention for my test classes.  I usually use the name of the class that I'm testing,
//followed by '_Test'
public class RelatedListEditController_Test {
    
    //setup method to create test data
    @TestSetup static void createTestData(){
        //insert whatever data that you want available for your tests here.
        //Example
        //create and insert test accounts
        Account testAccount = new Account(Name = 'Test Account 1');

        insert testAccount;

        //create and insert test contacts
        List<Contact> testContacts = new List<Contact>{
            new Contact(LastName = 'Test 1', AccountId = testAccount.Id),
            new Contact(LastName = 'Test 2', AccountId = testAccount.Id)
        };
        insert testContacts;

    }//end of testSetup

    //test the method for the records we created in the test setup
    @isTest static void testFetchRecords(){
        //get one of the test accounts
        Account testAccount = [SELECT Id FROM Account LIMIT 1];

        //create string of values to pass into method
        String values = testAccount.Id + ',Contact,AccountId';

        //execute the method
        List<Sobject> returnedList = RelatedListEditController.fetchRecords(values);

        //check our results
        System.assertEquals(2, returnedList.size());

        //check the object types to make sure they match
        for(SObject so: returnedList){
            System.assertEquals('Contact', so.getSObjectType().getDescribe().getName());
        }

        @isTest static void testEmptyResults(){
            //we would want a test here to test a scenario where we passed in correct values, but the query returned zero results
        }

        @isTest static void testNullResults(){
            //here we would pass in a parameter string without enough arguments and make sure we get a null returned.
        }

    }
}

 
This was selected as the best answer