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
aklkkaklkk 

how to write a test class for upload the data and and apex validation ?

Hi All 

Please help me how to write a test class for this code ??

public class FileUploaderTest
{
    public string nameFile{get;set;}
   
    public String newRetailName{get;set;}
    public Blob contentFile{get;set;}
    String[] filelines = new String[]{};
    public List<Lead> accstoupload;
    public String mainFileName;
    
    /***This function reads the CSV file and inserts records into the Account object. ***/
    public Pagereference ReadFile()
    {
        
         newRetailName = System.currentPageReference().getParameters().get('id');

        try{
                //Convert the uploaded file which is in BLOB format into a string
                //nameFile =blobToString( contentFile,'ISO-8859-1');
                //System.assert(false,nameFile);
            if(String.isNotBlank(nameFile) && !nameFile.containsIgnoreCase('.csv')){
                ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Please upload only .csv files');
                ApexPages.addMessage(errormsg);
            } else if(String.isNotBlank(nameFile) && nameFile.containsIgnoreCase('.csv')) {
                //Now sepatate every row of the excel file
                mainFileName =blobToString( contentFile,'ISO-8859-1');
                filelines = mainFileName.split('\n');
                //System.assert(false,filelines);
                //Iterate through every line and create a Account record for each row
                accstoupload = new List<Lead>();
                for (Integer i=1;i<filelines.size();i++)
                {
                    String[] inputvalues = new String[]{};
                    inputvalues = filelines[i].split(',');
                    Lead newLead = new Lead();
                    if(String.isBlank(inputvalues[0])){
                        ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'FirstName is missing.');
                        ApexPages.addMessage(errormsg);
                        break;
                    } else {
                        newLead.FirstName = inputvalues[0];   
                    }
                    
                    if(String.isBlank(inputvalues[1])){
                        ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'LastName is missing.');
                        ApexPages.addMessage(errormsg);
                        break;
                    } else {
                        newLead.LastName = inputvalues[1];
                    }
                      if(String.isBlank(inputvalues[7])){
                        ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Zipcode is missing.');
                        ApexPages.addMessage(errormsg);
                        break;
                    } else {
                        newLead.Zip_Code__c= inputvalues[7];
                    }
                    //System.assert(false,inputvalues[2] +'=='+ inputvalues[3]);
                    if(String.isBlank(inputvalues[2]) && String.isBlank(inputvalues[3])){
                        ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Please enter either Phone or Email.');
                        ApexPages.addMessage(errormsg);
                        break;
                    } else {
                        newLead.email= inputvalues[2];
                        newLead.Phone= inputvalues[3];
                    }
                    /*if(String.isBlank(inputvalues[3])){
                        ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Please enter either phone.');
                        ApexPages.addMessage(errormsg);
                        break;
                    } else {
                        newLead.Phone= inputvalues[3];
                    }*/
                     newLead.Address__c= inputvalues[4];
                     newLead.City__c= inputvalues[5];
                     newLead.State__c= inputvalues[6];
                     newLead.Sale__c = inputvalues[8];
                     newLead.LeadSource = 'sales';
                     newLead.Product__c= inputvalues[9];
                     newLead.RecordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Retail Lead').getRecordTypeId();
                    
                }
        }
                
        //Finally, insert the collected records
            if(accstoupload != null && !accstoupload.isEmpty()){
                Database.upsert(accstoupload,false); 
             // upsert accstoupload;
            }
            
        }
        catch (Exception e)
        {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured inserting the records'+e.getMessage()+e.getStackTraceString());
            ApexPages.addMessage(errormsg);
        }    
        return null;
    }

Thanks 
aklkk
Deepali KulshresthaDeepali Kulshrestha
Hi aklkk,

IN order to write a test class for the data upload you just need to query the data to make sure it actually gets into the database. You should also test on the redirect and for the apex validations, you can test the trigger as usual.
Please refer to the following code as it may be helpful in solving your query:

https://salesforce.stackexchange.com/questions/204069/how-to-unit-test-file-uploading
https://developer.salesforce.com/forums/?id=9060G000000XeETQA0
https://salesforce.stackexchange.com/questions/177012/how-do-you-assert-a-test-method-for-apex-validation

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha