PDI · Question #13
A developer needs to have records with specific field values in order to test a new Apex class. What should the developer do to ensure the data is available to the test?
The correct answer is B. Use Test.loadDataO < > and reference a CSV file in a static resource. To provide specific test data for an Apex class, a developer should use Test.loadData() to load records from a CSV file stored in a static resource.
Question
A developer needs to have records with specific field values in order to test a new Apex class. What should the developer do to ensure the data is available to the test?
Options
- AUse Anonymous Apex to create the required data.
- BUse Test.loadDataO < > and reference a CSV file in a static resource.
- CUse SOQL to query the org for the required data.
- DUse Test.loadDataO and reference a JSON file in Documents.
How the community answered
(30 responses)- A7% (2)
- B80% (24)
- C10% (3)
- D3% (1)
Why each option
To provide specific test data for an Apex class, a developer should use `Test.loadData()` to load records from a CSV file stored in a static resource.
Anonymous Apex is typically used for one-off code execution and debugging, not for providing reusable, deterministic test data for Apex test classes.
`Test.loadData()` is the recommended method to efficiently load large sets of structured test data into Apex test methods, enabling developers to define specific field values in a CSV file referenced from a static resource.
Relying on existing org data via SOQL queries in test methods is an anti-pattern because it makes tests non-deterministic and prone to failure in different environments.
`Test.loadData()` is designed to work exclusively with CSV files, not JSON files, and these files must be stored as static resources, not in the Documents folder.
Concept tested: Apex test data setup with Test.loadData()
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_data_testmethod.htm
Topics
Community Discussion
No community discussion yet for this question.