Monday 9 March 2015

Load Test Data from Static Resource

we write test class to test out functionality and in test class we create test data, but in Salesforce using Test.loadData method, you can populate data in our test method without writing any code.

To load test data from Static Resource we have to create a .csv file and upload it to Static Resource

copy and paste the below data and save the file in .csv extension.

Name,Website,Phone,BillingStreet,BillingCity,BillingState,BillingPostalCode,BillingCountry
sForceTest1,http://www.sforcetest1.com,(415) 901-7000,The Landmark @ One Market,San Francisco,CA,94105,US
sForceTest2,http://www.sforcetest2.com,(415) 901-7000,The Landmark @ One Market Suite 300,San Francisco,CA,94105,US
sForceTest3,http://www.sforcetest3.com,(415) 901-7000,1 Market St,San Francisco,CA,94105,US


go to Static Resource [ Setup -> Develop -> Static Resource ]
create new Static Resource record, give Static Resource Name as "Test Records"
and save it.

After saving the csv file in static resource go to Apex Class and write a test class with below code, save it and run test class to see the result.


@isTest
private class TestDataLoadClass {
    
    static testMethod void loadTestData() {
        List<sObject> ls = Test.loadData(Account.sObjectType, 'testRecords');
        
        system.assertEquals(ls.size(), 3);
        
        Account acc1 = (Account)ls[0];
        system.debug(' Account Name ' + acc1.Name);
        system.debug(' Account Name ' + acc1.Id);
    }
}














No comments:

Post a Comment