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
Kumar GKumar G 

How to write a test class for AuraEnabled Lightning controller

I have written a first test class for AuraEnabled Lightning controller but code is not covering for the class.
Please check my code in code sample and suggest me.
Class:
public class ctssupportform {
    
    public static List<Attachment> attachments;
    public static final Integer DEFAULT_ATTACHMENT_NO=1;
    public String sobjId {get; set;}
    public String Appname{get; set;}
    public static Map<String, BMCServiceDesk__Category__c> catMap ;
    
    @AuraEnabled
    public static id Savesupportform(Case casedata, String fileName, String base64Data, String contentType){

        id RcrdTypeid= Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('Agent Helpdesk').getRecordTypeId();
        System.debug('Casedata@@@@@@@@@@@@@@'+RcrdTypeid);
        Casedata.recordtypeid=RcrdTypeid;//'0120U0000000Aju';
        
        System.debug('Casedata@@@@@@@@@@@@@@'+Casedata);
        insert Casedata; 
        System.debug('Casedata@@@@@@@@@@@@@@'+Casedata);
        List<BMCServiceDesk__Category__c> lstcat = [Select Id,Name from BMCServiceDesk__Category__c limit 50000];
        
        catMap = new Map<String, BMCServiceDesk__Category__c>();
        
        for (BMCServiceDesk__Category__c c : lstcat ){
            catMap.put(c.Name, c);
            
        }  
        System.debug('catMap@@@@@@@@@@@@@@'+catMap);  
        
        List<BMCServiceDesk__Impact__c> lstimp = [Select Id,Name from BMCServiceDesk__Impact__c limit 4];
        
        Map<String, BMCServiceDesk__Impact__c> impmap = new Map<String, BMCServiceDesk__Impact__c>();
        
        for (BMCServiceDesk__Impact__c i : lstimp){
            impmap.put(i.Name, i);
            
        }  
        System.debug('impmap@@@@@@@@@@@@@@'+impmap);           
        
        List<BMCServiceDesk__Urgency__c> lsturg = [Select Id,Name from BMCServiceDesk__Urgency__c limit 4];
        
        Map<String, BMCServiceDesk__Urgency__c> urgmap = new Map<String, BMCServiceDesk__Urgency__c>();
        
        for (BMCServiceDesk__Urgency__c u : lsturg){
            urgmap.put(u.Name, u);
            
        }
        
           
        List<User> lstuser = [Select Id,Name from user limit 50000];
        
        Map<String, User> usermap = new Map<String, User>();
        
        for (User u1 : lstuser){
            usermap.put(u1.Name, u1);
            
        }     
              
        System.debug('urgmap@@@@@@@@@@@@@@'+usermap);
        
        System.debug('@@@@@@@@@@@@@@@@'+Casedata);
        base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');
        System.debug('@@@@@@@@@@@@@@@@'+base64Data );
        //base64Data = 'Test';
        Attachment oAttachment = new Attachment();
        oAttachment.parentId = casedata.id;
        oAttachment.Body = EncodingUtil.base64Decode(base64Data);        
        oAttachment.Name = fileName;
        oAttachment.ContentType = contentType;
        insert oAttachment; 
        
        BMCServiceDesk__Incident__c inc = new BMCServiceDesk__Incident__c();
        inc.Cases__c = casedata.id;
        inc.BMCServiceDesk__incidentDescription__c = casedata.Type_Of_Ticket__c;
        
        string newlowerstringcategory = (casedata.Applications__c).trim();
        System.debug('newlowerstringcategory@@@'+ newlowerstringcategory);
        system.debug('catMap.get(newlowerstringcategory)   '+catMap.get(newlowerstringcategory));
        if(catMap.get(newlowerstringcategory)!= null) {       
            inc.BMCServiceDesk__FKCategory__c  = catMap.get(newlowerstringcategory).id;
            System.debug('1111111111111111'+ catMap.get(newlowerstringcategory));
            System.debug('11111111111111111'+ inc.BMCServiceDesk__FKCategory__c);
        }

        string newlowerstringuser = casedata.OwnerId;
          System.debug('11111111111111111'+ newlowerstringuser);
            if(usermap.get(newlowerstringuser)!= null) {       
            inc.BMCServiceDesk__FKClient__c= usermap.get(newlowerstringuser).id;
            System.debug('1111111111111111'+ usermap.get(newlowerstringuser));
            System.debug('11111111111111111'+ inc.BMCServiceDesk__FKClient__c);
        }
        
        //inc.BMCServiceDesk__FKClient__c = casedata.OwnerId;
        
        inc.BMCServiceDesk__Service_Request_Title__c = casedata.Subject;
        System.debug('11111111111111111'+ inc.BMCServiceDesk__Service_Request_Title__c);
        
        string newlowerstringImpact = casedata.Impact__c.touppercase();
        System.debug('newlowerstringImpact '+ newlowerstringImpact );
        
        if(impmap.get(newlowerstringImpact) != null)        
            inc.BMCServiceDesk__FKImpact__c = impmap.get(newlowerstringImpact).id;
        System.debug('impmap_getnewlowerstringImpact_id'+ impmap.get(newlowerstringImpact).id );
        System.debug('11111111111111111'+ inc.BMCServiceDesk__FKImpact__c);
        
        
        string newupperstringUrgency = casedata.Urgency__c.touppercase();
        System.debug('newupperstringUrgency '+ newupperstringUrgency );
        if(urgmap.get(newupperstringUrgency) != null)
            inc.BMCServiceDesk__FKUrgency__c= urgmap.get(newupperstringUrgency).id;
        System.debug('11111111111111111'+ inc.BMCServiceDesk__FKUrgency__c);
        
        inc.BMCServiceDesk__incidentDescription__c= casedata.Description +' '+casedata.Type_Of_Ticket__c+' '+casedata.Justification__c;
        //inc.BMCServiceDesk__incidentDescription__c= casedata.Justification__c ;
        insert inc;
        System.debug('@@@@@@@@@@@@@@@@@@@@'+inc);
        
        Attachment oAttachment1 = new Attachment();
        oAttachment1.parentId = inc.id;
        oAttachment1.Body = EncodingUtil.base64Decode(base64Data);        
        oAttachment1.Name = fileName;
        oAttachment1.ContentType = contentType;
        insert oAttachment1; 
        System.debug('@@@@@@@@@@@@@@@@@@@@'+oAttachment1); 
        
        return casedata.id;
    }
}

Test Class :

@IsTest
Private class Test_ctssupportform
{
   @IsTest
    static void Savesupportform(){  
    
       Case cs  = new Case();
      
        cs.Status = 'open';
        cs.Helpdesk_Case_Resolution__c = 'Test 123';
        cs.Helpdesk_Case_Status__c = 'Closed';
        cs.Incident_Owner__c = 'Naveen';
        cs.Incident_Number__c  = '12345';
        Insert cs;
        
        
        Attachment attach=new Attachment();     
        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.body=bodyBlob;
        attach.parentId=cs.id;
        insert attach;
        
        List<Attachment> attachments=[select id, name from Attachment where parent.id=:cs.id];
        System.assertEquals(1, attachments.size()); 
        
        //ctssupportform ct = new ctssupportform(cs);
                 
    
    }   
}

Thanks in Advance...
Flint LockwoodFlint Lockwood
Your Apex class is not being called. You can keep the instantiation of the Case object:

Case cs  = new Case();
cs.Status = 'open';
cs.Helpdesk_Case_Resolution__c = 'Test 123';
cs.Helpdesk_Case_Status__c = 'Closed';
cs.Incident_Owner__c = 'Naveen';
cs.Incident_Number__c  = '12345';

Instantiate the ctssupportform class.

ctssupportform ct = new ctssupportform();
ct.Savesupportform(cs, filename, fileEncoded, type);

Pass in the filename, file (base64 encoded) and the file type along with the Case instantiation.