Salesforce Platform Developer I (legacy) — Question 79
How should a developer write unit tests for a private method in an Apex class?
Answer options
- A. Add a test method in the Apex class.
- B. Mark the Apex class as global.
- C. Use the SeeAllData annotation.
- D. Use the TestVisible annotation.
Correct answer: D
Explanation
The correct answer is D, as the TestVisible annotation allows private methods to be tested by making them visible to the test methods. Option A is incorrect because simply adding a test method does not enable access to private methods. Option B is wrong since marking a class as global changes its visibility but does not directly relate to testing private methods. Option C is also incorrect because SeeAllData is generally discouraged for unit testing due to potential data contamination.