You need to sign in to do that
Don't have an account?
How to write a Test method for a private method when the test method is in seperate test class.
Hi all, i am trying to write a test method for a private method and my test methods is in seperate test class.My methosd has a "private static" modifier but after writing test method as
static testMethod void test_getRestrictedProfileSet()
{
Test.startTest();
ProductsAndServicesController testgetprofile=new ProductsAndServicesController();
system.assert(testgetprofile.getRestrictedProfileSet()!=null);
Test.stopTest();
}
i am getting this error::::::Error: Compile Error: Method is not visible: [ProductsAndServicesController].getRestrictedProfileSet() at line 16 column 27...............................can anybody help me in solving this problem.It will be so helpful for me.
Try writing that testMethod in the same class as the private method you want to test, or else you will have to make that method public I believe.
You can create a public method specifically for testing in your class. For furthur optimisation, you can use Test.IsTestRunning and then call the private method.
@isTest
class PrivateCall
{
public static testMethod void testPrivate()
{
PrivateInvocation p1= new PrivateInvocation();
//p1.tryMe();// Error Here
p1.callMethod();
}
}
public class PrivateInvocation
{
private void tryMe()
{
System.debug(This method is private);
}
public void callMethod()
{
if(Test.isRunningTest())
{
tryMe();
}
}
}
Hi
U cannot test the private method directly in test class, but u just see where that method calling i.e that can be call within the other public method, when u call that public method both will be tested.
Hope this will helps u.
Regards,
Rajesh.
Hi,
Interesting thread.
Using static variable to skip tiggers in order to avoid SOQL limits won't have any affect on the user's using the system at that particular time? For instance a test run might take 10 sec, so for those 10 secs the triggers will be skipped for all calls, since the variable defined is static.
Isn't it better to use a custom label?
Thanks
Please go through the example in the link below.
http://salesforceglobe4u.blogspot.in/2016/01/testclass-for-private-methodsvariables.html
Let me know in case of any issues
You can use @TestVisible Annotation for your method for apex class. You can able to call the private method from your test class,
For example:
Apex Class:
@TestVisible private void example(){ //your code here }