Test.load() can be used to load Data from Static Resource in Salesforce Test Class. The Static Resource should contain a csv file.
1. Create a csv file with sample data. Example is available in https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_load_data.htm.
2. Create a Static Resource with the .csv extension file created in Step 1.
3. Use Test.loadData() to use the records from the Static Resource.
Sample Apex Code:
@isTest
private class CSVTest {
@TestSetup
static void loadDataFromStaticResource() {
List < sObject > listAccounts = Test.loadData(
Account.SObjectType,
'accountData'
);
}
@isTest
static void testAccountsFromStaticResource() {
List < Account > listAccounts = [
SELECT ID FROM Account
];
Assert.isTrue(
listAccounts.size() == 3,
'Expected 3 accounts'
);
}
}