How to write test code coverage for private methods in Salesforce?

How to write test code coverage for private methods in Salesforce?

TestVisible annotation 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.

Sample Class:

public class TestVisibleExample {
    // Private member variable
    @TestVisible private static Integer recordNumber = 1;


    // Private method
    @TestVisible private static void updateRec() {
    }
}  

Test Class:

@isTest
private class TestVisibleExampleTest {
    @isTest static void test1() {
        // Accessing private variable annotated with TestVisible
        Integer i = TestVisibleExample.recordNumber;
        System.assertEquals(1, i);

        // Accessing private method annotated with TestVisible
        TestVisibleExample.updateRecord();
    }
}

Cheers!!!

Leave a Reply