Salesforce Platform Developer I (legacy) — Question 150
Which approach should be used to provide test data for a test class?
Answer options
- A. Query for existing records in the database.
- B. Execute anonymous code blocks that create data.
- C. Use a test data factory class to create test data.
- D. Access data in @TestVisible class variables.
Correct answer: C
Explanation
The correct answer is C because a test data factory class allows for consistent and reusable creation of test data, which is crucial for reliable testing. The other options, while possible, may lead to dependency on existing data (A), less controlled data creation (B), or limited accessibility (D), which can complicate testing and reduce reliability.