Play Games

Search This Blog

Thursday, January 21, 2016

Testclass for private methods/variables

Testclass for private methods/variables

Solution: Use @TestVisible annotation before private variables/methods of main class.It wonot change any functionality of original class.
Example:
Initial main Apex Class:
public class PrivateMethodDemo {
    private void privateMethod() {
    }
}
In order to write test class for this,first modify this class like below
public class PrivateMethodDemo {
    @TestVisible private void privateMethod() {
    }
}
Now write test class for this
Test class:
@isTest
private class PrivateMethodDemoTest {
    public static testmethod void validate() {
        PrivateMethodDemo demo = new PrivateMethodDemo ();
        demo.privateMethod();
    }
}
Now when we run the test class it will cover all lines of main class.


No comments:

Post a Comment