How should a developer write unit tests for a private method in An Apex class?

How should a developer write unit tests for a private method in An Apex class?
A . Add a test method in the Apex class,
B . Use the SeeAllData annotation.
C . Use the @TestVisible annotation.
D . Mark the Apex class as global.

Answer: C

Explanation:

To write unit tests for a private method in an Apex class:

Option C: Use the @TestVisible annotation.

The @TestVisible annotation allows test methods to access private methods and variables in Apex classes. By annotating the private method with @TestVisible, it becomes accessible within test classes.

Example:

public class MyClass {

@TestVisible

private static void myPrivateMethod() {

// Implementation

}

}

Reference: "Use the @TestVisible annotation to allow test methods to access private or protected members of another class outside the test class."

― Apex Developer Guide: The @TestVisible Annotation Why Other Options Are Incorrect:

Option A: You cannot add test methods inside a non-test Apex class. Test methods must be in a test class annotated with @IsTest.

Option B: Using SeeAllData=true allows access to organization data but does not affect the visibility of private methods.

Option D: Marking the Apex class as global changes its access modifier but does not expose private methods to test methods.

Latest DEX-450 Dumps Valid Version with 456 Q&As

Latest And Valid Q&A | Instant Download | Once Fail, Full Refund

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments