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
StenderStender 

Test code for incoming CSV

Hello,


I am having trouble writing test code for an incoming .csv file.  My functioanlity actually has two steps, one where a .csv is createed by a VF page and another that will 'consume' the data from the .csv and update records in SF.  My idea was to use the test code for the 'export' portion of the project to be used as the test blobl for the 'import' portion.  However, in my test code I can't use the 'getContent()' method for the current page to assign the export page as a blob.  Any ideas on how else to do this?

 

Thanks,
Jeremy Stender

admintrmpadmintrmp

You will have to circumvent the process for retrieving the content of the CSV file and define a string that represents a sample for CSV content.

 

Make use of the Test.isRunningTest() in your Apex classes.

 

For example (with a static resource - but will be a similar concept for BLOB uploads too):

 

if ( Test.isRunningTest() ) {
      csv = 'field1,field2,field3';
} else {
      PageReference pr = new PageReference('/resource/package/import.csv');
      csv = pr.getContent().toString();
}

 

If you are using BLOB, convert your sample CSV file (a miniscule one will be best) into a BLOB string. Use that string and convert that back when you come to your test code.

StenderStender

Right, thats what I figured I needed to do.  Was basically mock up the .csv and assign it to a Blob.  My question though is how to indicate a 'new line' when creating the blob?.  Right now what I have done is assigned each 'row' of the csv file to a different string (either hard-coded or using the same get() methods that the VF Export page does), and then concatenated them all into a big string file that I use to assign the blob.  But I don't think it is working.  Here is the relevant code:

        String blobCreator = csvLine1 + '\r\n' + csvLine2 + '\r\n' + csvLine3 + '\r\n' + csvLine4 + '\r\n' + csvLine5 + '\r\n' + csvLine6 + '\r\n' + csvLine7 + '\r\n' + csvLine8 + '\r\n' + csvLine9 + '\r\n' + csvLine10 + '\r\n' + csvLine11 + '\r\n' + csvLine12;        
        
        
        //start test for Import(Upload) controller.  this portion changing the current page to the appropriate VF page
        PageReference pg2 = Page.ROIImport;
        pg2.getParameters().put('id', o.Id);
        pg2.getParameters().put('accountId', a.Id);
        Test.setCurrentPage(pg2);
        
        //Add parameters to page URL
        ApexPages.currentPage().getParameters().put('id', o.Id);
        ApexPages.currentPage().getParameters().put('accountId', a.Id);
        
        /*instantiate controller and assign the contentFile variable to the blob.  This 'contentFile' attribute is what the 'inputFile' on the VF page assigns the uploaded .csv file as.  This is also what the updateRecordsWithROICSV() method passes to the parser (returns a List<List<String>>) before actually making updates to records*/
        ROIUploadController uploadController = new ROIUploadController();
        uploadController.contentFile = blob.valueof(blobCreator);
        
        //run test methods - this method is what the command button on the VF page calls, and is the only really relevant one
        uploadController.updateRecordsWithROICSV();

 

The problem is that I am getting an error with the subsequent updates that happen, which first means that the assignment of the blobl and the 'parsing' of it happens correctly, but reading the debug log the it looks like it is just reading the file as on long row, rather than 12 separate rows like it should.  I think getting to blob to correctly reflect how a .csv file is written with the correct 'new line' entry is all I really need now.  Thanks again

 

Jeremy Stender

StenderStender

Error was being caused by something else.  I have finished the code and it is working now.  Thanks for the help!