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
AtiqahAtiqah 

code coverage if condition

Hi, Can help me with the code coverage for this code?
if (SelectedMonth == 'Jan'){
                    a.January__c = double.valueOf(inputvalues[1]);
                    a.January_SO_Number__c = inputvalues[2];
                }else if(SelectedMonth == 'Feb'){
                    a.February__c = double.valueOf(inputvalues[1]);
                    a.February_SO_Number__c = inputvalues[2];
                }else if(SelectedMonth == 'Mar'){
                    a.March__c = double.valueOf(inputvalues[1]);
                    a.March_SO_Number__c = inputvalues[2];
                }else if(SelectedMonth == 'Apr'){
                    a.April__c = double.valueOf(inputvalues[1]);
                    a.April_SO_Number__c= inputvalues[2];
                }else if(SelectedMonth == 'May'){
                    a.Mei__c = double.valueOf(inputvalues[1]);
                    a.May_SO_Number__c= inputvalues[2];
                }else if(SelectedMonth == 'Jun'){
                    a.June__c = double.valueOf(inputvalues[1]);
                    a.June_SO_Number__c= inputvalues[2];
                }else if(SelectedMonth == 'Jul'){
                    a.July__c = double.valueOf(inputvalues[1]);
                    a.July_SO_Number__c= inputvalues[2];
                }else if(SelectedMonth == 'Aug'){
                    a.August__c = double.valueOf(inputvalues[1]);
                    a.August_SO_Number__c= inputvalues[2];
                }else if(SelectedMonth == 'Sep'){
                    a.September__c = double.valueOf(inputvalues[1]);
                    a.September_SO_Number__c= inputvalues[2];
                }else if(SelectedMonth == 'Oct'){
                    a.October_del__c = double.valueOf(inputvalues[1]);
                    a.October_SO_Number__c= inputvalues[2];
                }else if(SelectedMonth == 'Nov'){
                    a.November__c = double.valueOf(inputvalues[1]);
                    a.November_SO_Number__c = inputvalues[2];
                }else if(SelectedMonth == 'Dec'){
                    a.December__c = double.valueOf(inputvalues[1]);
                    a.December_SO_Number__c = inputvalues[2];
                }

User-added imageThis is the full code
 
public class UpdateIncentiveFile {
    
    public string nameFile{get;set;}
    public Blob contentFile{get;set;}
    public String selectedYear {get; set;}
    public String SelectedMonth {get; set;}
    
    String[] filelines = new String[]{};
        List<Incentive__c> accstoupload;
    List<id> listOfId;
    
    //years dropdown
    public List<SelectOption> getYears() {
        List<SelectOption> options = new List<SelectOption>();
        for (Integer i = System.Today().year() - 2; i < System.Today().year() + 3; i++) {
            options.add(new SelectOption(String.valueOf(i), String.valueOf(i)));
        }
        return options;
    }
    
    //Month
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('--Select--','--Select--'));
        options.add(new SelectOption('Jan','January'));
        options.add(new SelectOption('Feb','February'));
        options.add(new SelectOption('Mar','March'));
        options.add(new SelectOption('Apr','April'));
        options.add(new SelectOption('May','May'));
        options.add(new SelectOption('Jun','June'));
        options.add(new SelectOption('Jul','July'));
        options.add(new SelectOption('Aug','August'));
        options.add(new SelectOption('Sep','September'));
        options.add(new SelectOption('Oct','October'));
        options.add(new SelectOption('Nov','November'));
        options.add(new SelectOption('Dec','December'));
        
        
        return options;
    }
    
    public UpdateIncentiveFile() {
        selectedYear = String.valueOf(System.Today().year());
        SelectedMonth = '--Select--';
        System.debug('The value is accstoupload: ' + SelectedMonth + selectedYear );
    }
    
    /***This function reads the CSV file and inserts records into the Incentive__c object. ***/
    public Pagereference ReadFile()
    {
        try{
            //Convert the uploaded file which is in BLOB format into a string
            nameFile =blobToString(contentFile,'ISO-8859-1');
            
            //Now sepatate every row of the excel file
            filelines = nameFile.split('\n');
            
            //Iterate through every line and create a Incentive__c record for each row
            accstoupload = new List<Incentive__c>();
            for (Integer i=1;i<filelines.size();i++)
            {
                String[] inputvalues = new String[]{};
                    inputvalues = filelines[i].split(',');
                
                
                Incentive__c a = new Incentive__c();
                //a.id= listOfId;
                a.Name = inputvalues[0];      
                a.Year__c = selectedYear;
                a.Unique_Record_Key__c = a.Name + a.Year__c;
                
                if (SelectedMonth == 'Jan'){
                    a.January__c = double.valueOf(inputvalues[1]);
                    a.January_SO_Number__c = inputvalues[2];
                }else if(SelectedMonth == 'Feb'){
                    a.February__c = double.valueOf(inputvalues[1]);
                    a.February_SO_Number__c = inputvalues[2];
                }else if(SelectedMonth == 'Mar'){
                    a.March__c = double.valueOf(inputvalues[1]);
                    a.March_SO_Number__c = inputvalues[2];
                }else if(SelectedMonth == 'Apr'){
                    a.April__c = double.valueOf(inputvalues[1]);
                    a.April_SO_Number__c= inputvalues[2];
                }else if(SelectedMonth == 'May'){
                    a.Mei__c = double.valueOf(inputvalues[1]);
                    a.May_SO_Number__c= inputvalues[2];
                }else if(SelectedMonth == 'Jun'){
                    a.June__c = double.valueOf(inputvalues[1]);
                    a.June_SO_Number__c= inputvalues[2];
                }else if(SelectedMonth == 'Jul'){
                    a.July__c = double.valueOf(inputvalues[1]);
                    a.July_SO_Number__c= inputvalues[2];
                }else if(SelectedMonth == 'Aug'){
                    a.August__c = double.valueOf(inputvalues[1]);
                    a.August_SO_Number__c= inputvalues[2];
                }else if(SelectedMonth == 'Sep'){
                    a.September__c = double.valueOf(inputvalues[1]);
                    a.September_SO_Number__c= inputvalues[2];
                }else if(SelectedMonth == 'Oct'){
                    a.October_del__c = double.valueOf(inputvalues[1]);
                    a.October_SO_Number__c= inputvalues[2];
                }else if(SelectedMonth == 'Nov'){
                    a.November__c = double.valueOf(inputvalues[1]);
                    a.November_SO_Number__c = inputvalues[2];
                }else if(SelectedMonth == 'Dec'){
                    a.December__c = double.valueOf(inputvalues[1]);
                    a.December_SO_Number__c = inputvalues[2];
                }

                accstoupload.add(a);
                System.debug('The value is accstoupload: ' + accstoupload);
            }
            Database.upsert(accstoupload, Incentive__c.Unique_Record_Key__c);
        }
        catch(Exception e){
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured reading the CSV file-- '+e.getMessage());
            ApexPages.addMessage(errormsg);
        }       
        //Finally, insert the collected records
        try{
            update accstoupload;
        }
        catch (Exception e)
        {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured inserting the records'+e.getMessage());
            ApexPages.addMessage(errormsg);
        }    
        return null;
    }
    
    /**** This function sends back to the visualforce page the list of account records that were inserted ****/ 
    public List<Incentive__c> getuploadedAccounts()
    {
        if (accstoupload!= NULL)
            if (accstoupload.size() > 0)
            return accstoupload;
        else
            return null;                    
        else
            return null;
    }

    public static String blobToString(Blob input, String inCharset){
        String hex = EncodingUtil.convertToHex(input);
        System.assertEquals(0, hex.length() & 1);
        final Integer bytesCount = hex.length() >> 1;
        String[] bytes = new String[bytesCount];
        for(Integer i = 0; i < bytesCount; ++i)
            bytes[i] =  hex.mid(i << 1, 2);
        return EncodingUtil.urlDecode('%' + String.join(bytes, '%'), inCharset);
    }
    
}
And this is the code coverage

@IsTest(SeeAllData=true)
public class FileUploaderIncentive_TestMethod {
    static testmethod void testfileupload(){
        StaticResource testdoc = [Select Id,Body from StaticResource where name ='testMethodCSVUpload'];
        FileUploaderIncentive testUpload = new FileUploaderIncentive();
        testUpload.contentFile= testdoc.Body;
        testUpload.ReadFile();
        testUpload.getuploadedAccounts();
    }
    static testmethod void testfileupdate(){
        StaticResource testdoc = [Select Id,Body from StaticResource where name ='testMethodCSVUpload'];
        UpdateIncentiveFile testUpload = new UpdateIncentiveFile();
        testUpload.contentFile= testdoc.Body;
        testUpload.ReadFile();
        testUpload.getuploadedAccounts();
    }
    static testmethod void testfileupdateOption(){
        UpdateIncentiveFile controller = new UpdateIncentiveFile();
        Test.startTest();
        List<SelectOption> options = controller.getYears();
        List<SelectOption> option = controller.getItems();
        Test.stopTest();
        //system.assertNotEquals(null, options, 'The collection should be instantiated');
        //system.assert(!options.isEmpty(), 'The collection should be populated');
    }
}




 
Best Answer chosen by Atiqah
CharuDuttCharuDutt
Hiii Atiqah
Try Below Code
@IsTest(SeeAllData=true)
public class FileUploaderIncentive_TestMethod {
    static testmethod void testfileupload(){
        StaticResource testdoc = [Select Id,Body from StaticResource where name ='testMethodCSVUpload'];
        FileUploaderIncentive testUpload = new FileUploaderIncentive();
        testUpload.contentFile= testdoc.Body;
        testUpload.SelectedMonth = 'Jan';
        testUpload.ReadFile();
        testUpload.getuploadedAccounts();
    }
    static testmethod void testfileupdate(){
        StaticResource testdoc = [Select Id,Body from StaticResource where name ='testMethodCSVUpload'];
        UpdateIncentiveFile testUpload = new UpdateIncentiveFile();
        testUpload.contentFile= testdoc.Body;
        testUpload.SelectedMonth = 'Feb';
        testUpload.ReadFile();
        testUpload.getuploadedAccounts();
    }
    static testmethod void testfileupdateOption(){
        UpdateIncentiveFile controller = new UpdateIncentiveFile();
        Test.startTest();
        List<SelectOption> options = controller.getYears();
        List<SelectOption> option = controller.getItems();
        Test.stopTest();
        //system.assertNotEquals(null, options, 'The collection should be instantiated');
        //system.assert(!options.isEmpty(), 'The collection should be populated');
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hiii Atiqah
Try Below Code
@IsTest(SeeAllData=true)
public class FileUploaderIncentive_TestMethod {
    static testmethod void testfileupload(){
        StaticResource testdoc = [Select Id,Body from StaticResource where name ='testMethodCSVUpload'];
        FileUploaderIncentive testUpload = new FileUploaderIncentive();
        testUpload.contentFile= testdoc.Body;
        testUpload.SelectedMonth = 'Jan';
        testUpload.ReadFile();
        testUpload.getuploadedAccounts();
    }
    static testmethod void testfileupdate(){
        StaticResource testdoc = [Select Id,Body from StaticResource where name ='testMethodCSVUpload'];
        UpdateIncentiveFile testUpload = new UpdateIncentiveFile();
        testUpload.contentFile= testdoc.Body;
        testUpload.SelectedMonth = 'Feb';
        testUpload.ReadFile();
        testUpload.getuploadedAccounts();
    }
    static testmethod void testfileupdateOption(){
        UpdateIncentiveFile controller = new UpdateIncentiveFile();
        Test.startTest();
        List<SelectOption> options = controller.getYears();
        List<SelectOption> option = controller.getItems();
        Test.stopTest();
        //system.assertNotEquals(null, options, 'The collection should be instantiated');
        //system.assert(!options.isEmpty(), 'The collection should be populated');
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
AtiqahAtiqah
I try to use your code, but the coverage is still not up to 75%
 
static testmethod void testfileupdate(){
        StaticResource testdoc = [Select Id,Body from StaticResource where name ='testMethodCSVUpload'];
        UpdateIncentiveFile testUpload = new UpdateIncentiveFile();
        testUpload.contentFile= testdoc.Body;
        testUpload.SelectedMonth = 'Dec';
        testUpload.ReadFile();
        testUpload.getuploadedAccounts();
    }
Let's say if I use this for Jan, Feb, ect then only can get up until 75%. But I need to repeat the code like this
 
static testmethod void testfileupdate(){
        StaticResource testdoc = [Select Id,Body from StaticResource where name ='testMethodCSVUpload'];
        UpdateIncentiveFile testUpload = new UpdateIncentiveFile();
        testUpload.contentFile= testdoc.Body;
        testUpload.SelectedMonth = 'Dec';
        testUpload.ReadFile();
        testUpload.getuploadedAccounts();
    }
    
    static testmethod void testfileupdate2(){
        StaticResource testdoc = [Select Id,Body from StaticResource where name ='testMethodCSVUpload'];
        UpdateIncentiveFile testUpload = new UpdateIncentiveFile();
        testUpload.contentFile= testdoc.Body;
        testUpload.SelectedMonth = 'Feb';
        testUpload.ReadFile();
        testUpload.getuploadedAccounts();
    }
    static testmethod void testfileupdate3(){
        StaticResource testdoc = [Select Id,Body from StaticResource where name ='testMethodCSVUpload'];
        UpdateIncentiveFile testUpload = new UpdateIncentiveFile();
        testUpload.contentFile= testdoc.Body;
        testUpload.SelectedMonth = 'Jun';
        testUpload.ReadFile();
        testUpload.getuploadedAccounts();
    }


 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Atiqah,

for each if condition you need to write seperate method.

Repeat the above steps and keep the matching for if else conditions you will get the coverage.
 
static testmethod void testfileupdate(){
        StaticResource testdoc = [Select Id,Body from StaticResource where name ='testMethodCSVUpload'];
        UpdateIncentiveFile testUpload = new UpdateIncentiveFile();
        testUpload.contentFile= testdoc.Body;
        testUpload.SelectedMonth = 'Dec';
        testUpload.ReadFile();
        testUpload.getuploadedAccounts();
    }
    
    static testmethod void testfileupdate2(){
        StaticResource testdoc = [Select Id,Body from StaticResource where name ='testMethodCSVUpload'];
        UpdateIncentiveFile testUpload = new UpdateIncentiveFile();
        testUpload.contentFile= testdoc.Body;
        testUpload.SelectedMonth = 'Feb';
        testUpload.ReadFile();
        testUpload.getuploadedAccounts();
    }
    static testmethod void testfileupdate3(){
        StaticResource testdoc = [Select Id,Body from StaticResource where name ='testMethodCSVUpload'];
        UpdateIncentiveFile testUpload = new UpdateIncentiveFile();
        testUpload.contentFile= testdoc.Body;
        testUpload.SelectedMonth = 'Jun';
        testUpload.ReadFile();
        testUpload.getuploadedAccounts();
    }
static testmethod void testfileupdate4(){
        StaticResource testdoc = [Select Id,Body from StaticResource where name ='testMethodCSVUpload'];
        UpdateIncentiveFile testUpload = new UpdateIncentiveFile();
        testUpload.contentFile= testdoc.Body;
        testUpload.SelectedMonth = 'July';
        testUpload.ReadFile();
        testUpload.getuploadedAccounts();
    }
static testmethod void testfileupdate5(){
        StaticResource testdoc = [Select Id,Body from StaticResource where name ='testMethodCSVUpload'];
        UpdateIncentiveFile testUpload = new UpdateIncentiveFile();
        testUpload.contentFile= testdoc.Body;
        testUpload.SelectedMonth = 'August';
        testUpload.ReadFile();
        testUpload.getuploadedAccounts();
    }
static testmethod void testfileupdate6(){
        StaticResource testdoc = [Select Id,Body from StaticResource where name ='testMethodCSVUpload'];
        UpdateIncentiveFile testUpload = new UpdateIncentiveFile();
        testUpload.contentFile= testdoc.Body;
        testUpload.SelectedMonth = 'September';
        testUpload.ReadFile();
        testUpload.getuploadedAccounts();
    }

// create remaining methods for other months



If this information helps, Mark it as best answer.

Thanks!!