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
Carolyn juliana 7Carolyn juliana 7 

How to Write a test class for Aura Enabled class as shown

hey gurus,

need your help in writing test class code for below apex class
 
public with sharing class SaveRecord_Lightning{
    
	@AuraEnabled
	public static List<ERT_Case_Type> getAllrecords(){
	 
	 return [SELECT Id,Case__c,Level1__c,Level2__c,Level3__c from ERT_Case_Type];
	 }
	 
	 @AuraEnabled
	 public static ERT_Case_Type saveRecord(ERT_Case_Type recordDetail){
	   upsert recordDetail;
	   return recordDetail;
	   
	 
	 }
	
	
	}

Your help is highlyappreicated
Carolyn
Best Answer chosen by Carolyn juliana 7
Andrew GAndrew G
The class seems simple so the test class could be as simple as:
@IsTest
private class SaveRecord_Lightning_Test {
    @IsTest
    static void testBehavior() {

//create a test record to be returned by the invoked class
        ERT_Case_Type ct = new ERT_Case_Type(Case__c='Test value', Level1__c='one',Level2__c='two',Level1__c='three');
        insert ct;

        Test.startTest();
        List<ERT_Case_Type> cts = SaveRecord_Lightning.getAllrecords();
        System.assertEquals(1, cts.size());

        ERT_Case_Type ctReturned = SaveRecord_Lightning.saveRecord(ct);
        System.assertEquals('Test Value', ctReturned.Case__c);
        Test.stopTest();
    }
}
Note, if Case__c is a lookup field to a case record, you will need to insert a Test Case Record, and therefore perhaps an Account record also. 

Regards
Andrew