Salesforce Certified Platform Developer free question 10 / 10 · Domain 4: Testing, Debugging, and Deployment
The class PriceCalc has a private static method, roundUp, that should stay hidden from outside classes. A developer wants a test class to call roundUp directly, while production classes still cannot call it. Which approach matches the Apex Developer Guide?
- Put @TestVisible on the test class, so that test class can call PriceCalc's private members without changing PriceCalc
- Add @TestVisible to roundUp, so tests can call it while other classes still see it as private
- Write the test class as an inner class of PriceCalc, so it can call the private method and is invisible from outside
- Change roundUp to protected and then add @TestVisible, since @TestVisible cannot be used on a private member
Show the answer and explanation
Answer: B
Why this is correct
"TestVisible Annotation" says that annotating a member with @TestVisible allows test methods to access private or protected members of another class outside the test class: methods, member variables, and inner classes. The wider access applies only while tests run, and access from non-test classes is unchanged. It lets you test without changing the access modifier to public.
Why the other options are wrong
"Change roundUp to protected and then add @TestVisible, since @TestVisible cannot be used on a private member" … @TestVisible can be put on a private member. The guide describes it as exposing private and protected members to tests, and its example puts it directly on a private static variable and method. The modifier does not need to change.
"Put @TestVisible on the test class, so that test class can call PriceCalc's private members without changing PriceCalc" … The guide puts @TestVisible on the definition of the member to be exposed, such as a private variable or method. It is not placed on the test class to open another class's private members.
"Write the test class as an inner class of PriceCalc, so it can call the private method and is invisible from outside" … A class annotated with @IsTest must be a top-level class, so the test class cannot be written as an inner class of PriceCalc.
Worth remembering
@TestVisible widens access only for test calls, so production code can keep its private design. An @IsTest class can be private or public, must be top-level, and cannot be an interface or an enum.