+ Start a Discussion
Kumar GKumar G 

Code coverage issue for Lightning controller

I have written a test class to cover lightning controller , i am getting only 5% code coverage.
Please check my code in code sample and suggest me how to improve remaining code coverage.
If i did any mistake correct me.
 
controller : 

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(SeeAllData=true)
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;
        update cs;*/
        
        case cs1 = [Select id,Status,Helpdesk_Case_Resolution__c,Helpdesk_Case_Status__c,Incident_Owner__c,Incident_Number__c from case Limit 1];
        BMCServiceDesk__Category__c cat = new BMCServiceDesk__Category__c();
        cat.Name='SAR';
        cat.BMCServiceDesk__AvailableForIncidents__c=true;
        cat.BMCServiceDesk__inactive__c=false;
        insert cat;
        //update cat;
        
        BMCServiceDesk__Incident__c inc = new BMCServiceDesk__Incident__c();
        inc.Assigned_Staff__c='005800000045QSjAAM';
        inc.Resolution_Summary__c='resolved';
        inc.Service_Request_Form_Type__c='SAR';
        inc.BMCServiceDesk__incidentDescription__c='FSJ SAR test 123';
        inc.Resource_Type__c='New Contractor';
        inc.Start_Date__c = Date.Today();
        inc.SRF_Location__c='Australia';
        inc.Cases__c = cs1.id;
        inc.BMCServiceDesk__FKCategory__c=cat.Id;
        try{
           insert inc;
           //Update Inc;
        }catch(System.LimitException e){
        }
        
        Attachment attach=new Attachment();     
        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.body=bodyBlob;
        attach.parentId=cs1.id;
        insert attach;
       // update attach;
        
        List<Attachment> attachments=[select id, name from Attachment where parent.id=:cs1.id];
        System.assertEquals(1, attachments.size());
        
        string filename = 'Testfile';
        string fileEncoded = 'UTF-8 ';
        string type = 'Test';
        try{
         ctssupportform.Savesupportform(cs1,filename, fileEncoded, type); 
        }catch(System.DmlException e){
        }
        
    }       
}

Thanks in advance
Shivdeep KumarShivdeep Kumar
Hi,
Please try below code :
@IsTest()
Private class Test_ctssupportform
{
   @IsTest
    static void Savesupportform()
    {      
	id RcrdTypeid= Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('Agent Helpdesk').getRecordTypeId();
        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';
	cs.recordtypeId =RcrdTypeid ;
        Insert cs;
        
        
        BMCServiceDesk__Category__c cat = new BMCServiceDesk__Category__c();
        cat.Name='SAR';
        cat.BMCServiceDesk__AvailableForIncidents__c=true;
        cat.BMCServiceDesk__inactive__c=false;
        insert cat;
        
	BMCServiceDesk__Impact__c impact = New BMCServiceDesk__Impact__c();
	impact.Name ='impact 1' ;
	insert impact;

	BMCServiceDesk__Urgency__c urgency = New BMCServiceDesk__Urgency__c();
	urgency.Name = 'urgency 1';
	insert urgency;
	
	User u = New User();
	// insert all user field
	// try to run as user
	
        BMCServiceDesk__Incident__c inc = new BMCServiceDesk__Incident__c();
        inc.Assigned_Staff__c='005800000045QSjAAM';
        inc.Resolution_Summary__c='resolved';
        inc.Service_Request_Form_Type__c='SAR';
        inc.BMCServiceDesk__incidentDescription__c='FSJ SAR test 123';
        inc.Resource_Type__c='New Contractor';
        inc.Start_Date__c = Date.Today();
        inc.SRF_Location__c='Australia';
        inc.Cases__c = cs1.id;
        inc.BMCServiceDesk__FKCategory__c=cat.Id;
        try{
           insert inc;
           //Update Inc;
        }catch(System.LimitException e){
        }
        
        Attachment attach=new Attachment();     
        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.body=bodyBlob;
        attach.parentId=cs1.id;
        insert attach;
       // update attach;
        
        List<Attachment> attachments=[select id, name from Attachment where parent.id=:cs1.id];
        System.assertEquals(1, attachments.size());
        
        string filename = 'Testfile';
        string fileEncoded = 'UTF-8 ';
        string type = 'Test';
        try{
         ctssupportform.Savesupportform(cs1,filename, fileEncoded, type); 
        }catch(System.DmlException e){
        }
        
    }       
}

Please let me know if this help

Thanks
Shivdeep​