Code coverage in Salesforce has always been a pain. One of the major problems in completing code coverage is creating test data. When Sobjects have multiple or complex relations with other Sobjects, it becomes tough to create accurate data.
Salesforce has an inbuilt feature which can rescue us from doing redundant work. Using this feature we can load test data from an excel sheet. For this tutorial, we will first export org data and will use the same in test class.
Step 1: Export data by Setup -> Data Export. Select Account and click on Export. You will receive exported file by Email.
Step 2: Open Excel sheet and remove column like isDeleted, ParentId, CreatedBy, ModifiedBy. I am removing those because these may create problem while creating data. So it is better if we remove the column which we do not require. I removed the ParentId field because that field has to be first present in the system, You can keep it but make sure the first record in the Excel sheet is your parent record.
Step 3: Create a static resource named "AccountTestData".
Step 4: Apex Classes
SampleApexClass for which we are creating test class
SampleApexClassTest
Code Coverage ->
From now on, Create data in your developer org, Export that data and use the same in test class.
Comment below if this approach saves your time and effort.
Kommentare