+ Start a Discussion
aastha bhalla 2aastha bhalla 2 

Test Class coverage!! I have this class which is getting the data from LWC js controller, in which i am creating a record along with uploading a file which gets attached to the very same record. I am only getting 57% coverage. Please help!!

==============Apex Class=================
public without sharing class contactClass {    
        @AuraEnabled
        public static string saveContact(Lead LeadRec, string file, string fileName) {
          string LeadID;
          try{
            Lead l = new Lead();
            list<Lead> leadList = [select Id, Email from Lead where Email =:LeadRec.Email];
            system.debug('EmailListLength  ' + leadList.size()); 

            Contact c = new Contact();
            list<Contact> conList = [select Id, Email from Contact where Email =:LeadRec.Email];
            system.debug('EmailListLength  ' + conList.size()); 
                      
            if(leadList.size() > 0 || conList.size() > 0 ) {
            system.debug('Duplicate email Not Allowed');    
            //return 'Duplicate email not allowed';        
            throw new AuraHandledException('First name is required');      
            }                               
            else {      
            Insert LeadRec;      
            }    
            LeadID = LeadRec.Id;
            String base64File = EncodingUtil.urlDecode(file, 'UTF-8');
            ContentVersion contentVersionRec = new ContentVersion();
            contentVersionRec.Title = fileName;
            contentVersionRec.PathOnClient = '/' + fileName;
            contentVersionRec.FirstPublishLocationId = LeadRec.Id;
            contentVersionRec.VersionData =      EncodingUtil.base64Decode(base64File);
            contentVersionRec.IsMajorVersion = true;
            Insert contentVersionRec;
            
          }
          catch(Exception ex){
            system.debug('Exception===>'+ex.getMessage());
          }
          return LeadID;
        }        
      }


===================TEST CLASS=======================
@isTest
public class contactClassTest {
    @isTest
     static void positiveCheckMethod(){
        Lead objLead = New Lead();
        objLead.LastName = 'Test';
        objLead.FirstName='Upload';
        objLead.Company = 'Test Company';
        objLead.status='New';
        objLead.School__c='VVPS';
        objLead.Sport__c='Cricket';
        objLead.Email='abc@gmail.com';
        objLead.Phone='9988776655';
        insert objLead;
        
        contact objCon = new contact();
        objCon.FirstName='Test 1';
        objCon.Email='abcdef@gmail.com';
        objCon.LastName='Upload';
        objCon.Phone='1234567876';
        insert objCon;
            
        ContentVersion cv = new ContentVersion();
        cv.Description  = 'test description';
        cv.PathOnClient ='test_file.txt';
        cv.Title = 'test file '+DateTime.now();
        cv.versiondata=Blob.valueOf('test file body');
        cv.FirstPublishLocationId = objLead.Id;
        insert cv;
        system.debug(cv);
        
         Blob csvFileBody =cv.VersionData;
         String csvAsString= csvFileBody.toString();
         system.debug(csvAsString);
        
        
        
        contactClass.saveContact(objLead,csvAsString,'test file');
        

           
    }
      @isTest
      public static void negativCheckMethod(){
        lead l1 = new lead();
        contactClass.saveContact(l1,'demo','test file');
    }   
}
MenteeMentee
 Please write a scenario for duplicate records (i.e.,  if(leadList.size() > 0 || conList.size() > 0 )  and do assert statement. May be it will help with some coverage.
If still not resolved please provide a screenshot what is not covered
ravi soniravi soni
Hi Aastha,
try the below test class with 100% coverage.
@isTest
public class contactClassTest {
    @isTest
     static void positiveCheckMethod(){
        Lead objLead = New Lead();
        objLead.LastName = 'Test';
        objLead.FirstName='Upload';
        objLead.Company = 'Test Company';
        objLead.status='New';
        //objLead.School__c='VVPS';
        //objLead.Sport__c='Cricket';
        objLead.Phone='9988776655';
       
         string leadId = contactClass.saveContact(objLead,'myImg','test file');
          list<Lead> leadList = [select Id, Email from Lead where Id =: leadId];
          system.assertEquals(leadList[0].Id, leadId);
        

           
    }
      @isTest
      public static void negativCheckMethod(){
       
        Lead objLead = New Lead();
        objLead.LastName = 'Test';
        objLead.FirstName='Upload';
        objLead.Company = 'Test Company';
        objLead.status='New';
        objLead.Phone='9988776655';
          
        contact objCon = new contact();
        objCon.FirstName='Test 1';
       // objCon.Email='abcdef@gmail.com';
        objCon.LastName='Upload';
        objCon.Phone='1234567876';
        insert objCon;
       
        contactClass.saveContact(objLead,'demo.jpg','test file');
    }   
}

don't forget to mark it as the best answer.
Thank you
aastha bhalla 2aastha bhalla 2
@Mentee 
  LeadID = LeadRec.Id;
            String base64File = EncodingUtil.urlDecode(file, 'UTF-8');
            ContentVersion contentVersionRec = new ContentVersion();
            contentVersionRec.Title = fileName;
            contentVersionRec.PathOnClient = '/' + fileName;
            contentVersionRec.FirstPublishLocationId = LeadRec.Id;
            contentVersionRec.VersionData = EncodingUtil.base64Decode(base64File);
            contentVersionRec.IsMajorVersion = true;
            Insert contentVersionRec;

following lines are not being covered by my code.
aastha bhalla 2aastha bhalla 2
@ravi soni ,
this is giving me a list exception. 'System.ListException: List index out of bounds' error 
I tried insert the objLead. Still giving me the same error. 
My contentVersion object lines are not getting covered, any idea how i can do that?
ravi soniravi soni

hi  aastha,
did you change something in my code? because it was giving me 100% coverage. you can uncomment the below 2 lines from the code and try again but don't fill email filed otherwise it will not work.
//objLead.School__c='VVPS';
//objLead.Sport__c='Cricket';

let me know about your status.
Thank you