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
JohnDuraiJohnDurai 

Test class for Custom component controller class

Hi All - Below is the controller class for my custom component, can someone help me with test class for this controller. Thanks!

public inherited sharing class CFRNotesControllerPOC {
    public class CFRNotesControllerException extends Exception {}

    @AuraEnabled(cacheable=true)
    public static NotesFieldWrapper getFieldInfo(String recordId, String fieldName){
        return new NotesFieldWrapper(recordId, fieldName);
    }

    @AuraEnabled
    public static void saveNotesInfo(String recordId, String fieldName, String fieldValue){
        SObject sobjectRecord = ((Id)recordId).getSObjectType().newSObject(recordId);
        sobjectRecord.put('Id', recordId);
        sobjectRecord.put(fieldName, fieldValue);
        Database.update(sobjectRecord);
    }

    public class NotesFieldWrapper {
        @auraEnabled
        public String fieldValue;
        @AuraEnabled
        public String fieldDescribe;
        //to check FLS
        @AuraEnabled
        public Boolean fieldAccess;
            
        public NotesFieldWrapper(String recordId, String fieldName) {
            Schema.DescribeSObjectResult objDescribe = ((Id)recordId).getSObjectType().getDescribe();
            String objName = objDescribe.getName();

            Map<String, Schema.SObjectField> fields = objDescribe.fields.getMap();
            if(String.isNotEmpty(fieldName)){
                Schema.SObjectField fieldToken = fields.get(fieldName);
                this.fieldDescribe = JSON.serialize(fieldToken.getDescribe());
                this.fieldAccess = fieldToken.getDescribe().isAccessible();

                String query = 'SELECT Id, ' + fieldName + ' FROM ' + objName + ' WHERE Id = \'' + recordId + '\'';
                SObject sobj = (SObject) Database.query(query);
                if(sobj != null){
                    this.fieldValue = (String) sobj.get(fieldName);
                }
            }
        }
    }
AnudeepAnudeep (Salesforce Developers) 
To get started, please use the following code
@isTest
private class TestCFRNotesControllerPOC
{
    @isTest
    static void TestForgotPassword()
    {
        String result = CFRNotesControllerPOC.getFieldInfo('test@test.test', 'pass');
        System.assertEquals(null, result);

String result1 = CFRNotesControllerPOC.saveNotesInfo('test@test.test', 'pass', 'testabc'); 

       System.assertEquals(null, result);
    }
}

Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you