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 LWC 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.

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);
                }
            }
        }
    }
Best Answer chosen by JohnDurai
Maharajan CMaharajan C
@isTest
public class CFRNotesControllerPOCTest {
    static testmethod void getFieldInfoTest(){
        Account testAccount = testDataFactory.createAccount();
        Note testNote = testDataFactory.createNote();
        
        Test.startTest();
            CFRNotesControllerPOC.getFieldInfo(testNote.Id,'Title');
            CFRNotesControllerPOC.saveNotesInfo(testNote.Id,'Title','Test Account Note Updated');
        Test.stopTest();
    }
}


public class testDataFactory{
	public static Account createAccount(){
		Account testAccount = new Account();
        testAccount.Name = 'Test Account';
        insert testAccount;
		return testAccount;
	}
	
	public static Note createNote(){
		Note testNote = new Note();
        testNote.Title = 'Test Account Note';
        testNote.Body = 'Test Account Note Body.';
        testNote.ParentId = testAccount.Id;
        insert testNote;
		return testNote;
	}
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi John,

Try the below test class:
 
@isTest
public class CFRNotesControllerPOCTest {
    static testmethod void getFieldInfoTest(){
        Account testAccount = new Account();
        testAccount.Name = 'Test Account';
        insert testAccount;

        Note testNote = new Note();
        testNote.Title = 'Test Account Note';
        testNote.Body = 'Test Account Note Body.';
        testNote.ParentId = testAccount.Id;
        insert testNote;
        
        Test.startTest();
            CFRNotesControllerPOC.getFieldInfo(testNote.Id,'Title');
            CFRNotesControllerPOC.saveNotesInfo(testNote.Id,'Title','Test Account Note Updated');
        Test.stopTest();
    }
}

Thanks,
Maharajan.C​
​​​​​​
JohnDuraiJohnDurai
Hi @Maharajan C - I tried by this way, but I need factory class method to for all insertaion operation, just thinking about that scenario
Maharajan CMaharajan C
@isTest
public class CFRNotesControllerPOCTest {
    static testmethod void getFieldInfoTest(){
        Account testAccount = testDataFactory.createAccount();
        Note testNote = testDataFactory.createNote();
        
        Test.startTest();
            CFRNotesControllerPOC.getFieldInfo(testNote.Id,'Title');
            CFRNotesControllerPOC.saveNotesInfo(testNote.Id,'Title','Test Account Note Updated');
        Test.stopTest();
    }
}


public class testDataFactory{
	public static Account createAccount(){
		Account testAccount = new Account();
        testAccount.Name = 'Test Account';
        insert testAccount;
		return testAccount;
	}
	
	public static Note createNote(){
		Note testNote = new Note();
        testNote.Title = 'Test Account Note';
        testNote.Body = 'Test Account Note Body.';
        testNote.ParentId = testAccount.Id;
        insert testNote;
		return testNote;
	}
}

Thanks,
Maharajan.C
This was selected as the best answer