I have a visualforce page controller class which has a soql in the beginning

select <fields> from <custom_object_A> where appnum = <fetched_from_vf_page_parameter>;

The controller class has lot of calculations after that soql. I am trying to create a test class. Since i want the test class to pass through various scenarios of the controller class, i will have to pass a lot of records to the custom_object_A.

I am wondering if i can use a csv file with records from the custom_object_A and upload as a static resource? I am not sure if i will have to insert the data in my custom_object_A in the test class or i can use the the static resource and loop through the rows?

2 Answers 2


You can indeed. The functionality you're looking for is Test.loadData(). From that documentation, the steps to use it are

  1. Add the data in a .csv file.
  2. Create a static resource for this file.
  3. Call Test.loadData within your test method and passing it the sObject type token and the static resource name.

You can call Test.loadData() in your @isTest test method or your @testSetup initialization method. You won't need to write any inserts or touch the CSV content yourself.


Tried the following on the case object and it worked. Thank you David!

Controller Class

public class caseextn { public case case1{get;set;} public caseextn (ApexPages.StandardController stdController) { string casenum= ApexPages.currentPage().getParameters().get('casenum'); system.debug('casenum' + casenum); try{ case1 = [Select ownerid,id from case where casenumber = :casenum]; System.debug('case values'+ case1 );

    catch(exception e){
        system.debug('In ERROR No exception calculator record found for this application.');
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'No exception calculator record found for the application: ' + casenum+'.')); 
} //constructor end



Test Class

@isTest private class casetest {

private static testMethod void test() {
List caselst = Test.loadData(case.sObjectType, 'case'); system.debug('caselst '+caselst);
List caselst1 = (list)caselst;


for (case case1 : caselst1){
PageReference pageRef = Page.caseitemsvfpage; // Add your VF page Name here
system.debug('pageRef1 '+pageRef);
pageRef.getParameters().put('casenum', String.valueOf(case1.caseNumber)); 
ApexPages.StandardController sc = new ApexPages.StandardController(case1);
caseextn ece = new caseextn(sc);   
system.debug('pageRef3 '+pageRef);  

Test.StopTest(); } }

case static resource file:

"CaseNumber","Subject","Status","Priority" "00001002","Seeking guidance on electrical wiring installation for GC5060","New","Low" "00001016","Maintenance guidelines for generator unclear","New","Low" "00001024","Design issue with mechanical rotor","New","Low"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .