function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rahulrahxrahulrahx 

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.

SLockardSLockard

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.

SammyComesHereSammyComesHere

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();
}
}
}

Rajesh SriramuluRajesh Sriramulu

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.

Nas2482Nas2482

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

nikam.satishs1.3971116976725576E12nikam.satishs1.3971116976725576E12
Test methods are defined in a test class, separate from the class they test. This can present a problem when having to access a private class member variable from the test method, or when calling a private method. Because these are private, they aren’t visible to the test class. You can either modify the code in your class to expose public methods that will make use of these private class members, or you can simply annotate these private class members with TestVisible. When you annotate private or protected members with this annotation, they can be accessed by test methods and only code running in test context. e.g. @TestVisible private String privateMethod(Employee e) ,@TestVisible private Integer recordNumber = 0;
sunny522sunny522
Hi rahulrahx,
        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
Vadivel Murugan 9Vadivel Murugan 9
Hi rahulrahx,

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 }
Shane KenyonShane Kenyon
@TestVisible annotation docs: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_testvisible.htm