Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
TestVisible Annotation
Use the TestVisible annotation to allow test methods to access private or protected members of another class outside the test class. These members include methods, member variables, and inner classes. This annotation enables a more permissive access level for running tests only. This annotation doesn’t change the visibility of members if accessed by non-test classes.
With this annotation, you don’t have to change the access modifiers of your methods and member variables to public if you want to access them in a test method. For example, if a private member variable isn’t supposed to be exposed to external classes but it must be accessible by a test method, you can add the TestVisible annotation to the variable definition.
This example shows how to annotate a private class member variable and private method with TestVisible.
1public class TestVisibleExample {
2 // Private member variable
3 @TestVisible private static Integer recordNumber = 1;
4
5 // Private method
6 @TestVisible private static void updateRecord(String name) {
7 // Do something
8 }
9}This test class uses the previous class and contains the test method that accesses the annotated member variable and method.
1@IsTest
2private class TestVisibleExampleTest {
3 @IsTest static void test1() {
4 // Access private variable annotated with TestVisible
5 Integer i = TestVisibleExample.recordNumber;
6 System.assertEquals(1, i);
7
8 // Access private method annotated with TestVisible
9 TestVisibleExample.updateRecord('RecordName');
10 // Perform some verification
11 }
12}