+ Start a Discussion
chanti kchanti k 

How to i will get the code coverage for below test class

HI , 

i got only 60% code coverage, i need more than 75%. please let me know how  i will cover for loop condtion

 
@istest
public class importDataFromCSVController_test
{
 
 static testmethod void test()
 {
 
 importDataFromCSVController mvc = new importDataFromCSVController();
 mvc.importCSVFile();
 mvc.csvAsString='test';
 
 
 }
}


CLASS

public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}
public List<account> acclist{get;set;}
  public importDataFromCSVController(){
    csvFileLines = new String[]{};
    acclist = New List<Account>(); 
  }
  
  public void importCSVFile(){
       try{
           csvAsString = csvFileBody.toString();
           csvFileLines = csvAsString.split('\n'); 
            
           for(Integer i=1;i<csvFileLines.size();i++){
               Account accObj = new Account() ;
               string[] csvRecordData = csvFileLines[i].split(',');
               accObj.name = csvRecordData[0] ;             
               accObj.accountnumber = csvRecordData[1];
               accObj.Type = csvRecordData[2];
               accObj.AccountSource = csvRecordData[3];   
               accObj.Industry = csvRecordData[4];                                                                             
               acclist.add(accObj);   
           }
        //insert acclist;
        }
        catch (Exception e)
        {
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');
            ApexPages.addMessage(errorMessage);
        }  
  }
}


Please see the my screen shot

User-added image

Thanks,
Chanti
Kai Herng LauKai Herng Lau
Hi Chanti,

What you missing in the test class is to specify the csvFileBody. Below is my example for the test class:

@istest
public class importDataFromCSVController_test{
 
    static testmethod void test(){
    
        importDataFromCSVController mvc = new importDataFromCSVController();
        Blob bodyBlob=Blob.valueOf('Unit Test CSV Body \n new line');
        mvc.csvFileBody = bodyBlob;
        mvc.importCSVFile();
        mvc.csvAsString='test';
    }
}

Hope this help.