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
FreddySFreddyS 

Apex Test class for list update

Hello, can someone point me in the right direction on writing a test class for this apex controller (for a lightning component DataTable). Below is my controller and test class so far. My test class is covering 55% and covers the getRecords soql method but I cannot get the test class to cover the update list method (database.update).

The objects parent, child, and grandchild are in masterdetil relationships and require lookups to one another so there will always be a parent, child, and grandchild in the data model. 

Thanks so much!
 
Controller: 

public class LightningDataTableCtrl {
    @AuraEnabled    
    public static List <grandchild__c> getRecords (String parentId) {   
        List <grandchild__c> updatedObjList = 
            [SELECT Id,
             Name,
             Text_Formula__c,
             Status_Picklist__c,
             Hours_Number_Field__c,
             Custom_Lookup__c,
             Number_Field__c,
             Number_Field_2__c,
             Custom_Picklist__c
             FROM grandchild__c
             WHERE child__r.parent__c = :parentId
             ORDER BY Name ASC];
        return updatedObjList;
    }    
    @AuraEnabled    
    public static void updateRecords( List <grandchild__c> updatedObjList ) {    
        try {  
            Database.update (updatedObjList); 
            
        } catch(Exception e) {  
            
        }  
    }  
}

Test Class: 

@isTest
public class LightningDataTableCtrlTest {
    @isTest (SeeAllData = 'true') static void testGetRecords() {
       
        parent__c parent = new parent__c(Name = 'Lightning Data Table Apex Test', 
                                                                          parent_type_picklist__c = 'Duration');
        insert parent;
        
        child__c child = new child__c(Name = 'Apex Test Phase', 
                                                                                    parent__c = parent.Id);
        insert phase;
        
        grandchild__c grandchild = new grandchild__c(Name = 'Apex Test Task',
                                                                                 child__c = child.Id,
                                                                                 Number_field__c = 1, 
                                                                                 Date_field__c = Date.newInstance(2018, 4, 27),
                                                                                 Number_Field_2__c = 1);
        insert task;
        // List covers the soql and 'getRecords' method of the class.
        List <grandchild__c> getRecords = LightningDataTableCtrl.getRecords(granchild.id);
        
        // Test method to cover update method in controller        
    }
}

 
Best Answer chosen by FreddyS
Prakhar Saxena 19Prakhar Saxena 19
Hi FreddyS,

You can create another test method for updateRecords method.
 
@isTest
static void testUpdateRecords(){

   List<grandchild__c> updatedObjList = new List<grandchild__c>();
   LightningDataTableCtrl.updateRecords(updatedObjList);

}

Additionally, if you want to pass the same updatedObjList that you inserted in the testGetRecords method, then you will have to use @testSetup annotation for testGetRecords method.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm

If you want to use any test data in other test methods, then insert all such data in a method with @testSetup annotation and query the same in other methods. With @testSetup annotation, you can create test records once and access them in every test method.

Thanks.
Prakhar
 

All Answers

FreddySFreddyS
I need coverage on the updateRecords list update method on the controller.
Prakhar Saxena 19Prakhar Saxena 19
Hi FreddyS,

You can create another test method for updateRecords method.
 
@isTest
static void testUpdateRecords(){

   List<grandchild__c> updatedObjList = new List<grandchild__c>();
   LightningDataTableCtrl.updateRecords(updatedObjList);

}

Additionally, if you want to pass the same updatedObjList that you inserted in the testGetRecords method, then you will have to use @testSetup annotation for testGetRecords method.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm

If you want to use any test data in other test methods, then insert all such data in a method with @testSetup annotation and query the same in other methods. With @testSetup annotation, you can create test records once and access them in every test method.

Thanks.
Prakhar
 
This was selected as the best answer
FreddySFreddyS
Thanks so much! I was so close and your method got me to 88%!