Play Games

Search This Blog

Friday, April 28, 2017

Loading Test Data in test class from static resource in Salesforce.

Step 1: create a .csv file for the records you want to load.
If you want to load Account records,then in the first row,you need to give field names(Api Name only) and then you can add as many rows(records) you want to add.

Step 2: Create a static resource and then upload that .csv file(for example:Accounts)

Step 3: In your test class,you can use Test.loadData() to populate test Accounts.
Sample Code:
@isTest
private class TestingLOadData {
     static testmethod void testLoadData() {
        // Using Test.loadData method,we are loading Account records.
        List<sObject> lstAccount = Test.loadData(Account.sObjectType, 'Accounts');
        Account objAccount1 = (Account)lstAccount[0];
        System.debug('Account Record:'+objAccount1);
    }
}
Output: Once you run this test class,you can check debug logs

No comments:

Post a Comment