0

Can you help me to solve the issue and understand it better as I am new to Coding world.

I know the issue is because the Apex Class Method's Access modifier "Private" causing the trouble, as I am not able to access it from from Test Methods in Test class

Apex Class [Method DateWithin30Days is Private]

public class verifyDate{

    public static Date CheckDates(Date date1, Date date2){
    if(DateWithin30Days(date1,date2))
        return date2;
    else
        return SetEndOfMonthDate(date1);
    }

    private static Boolean DateWithin30Days(Date date1, Date date2){ //Private
    Date date30Days = date1.addDays(30); 
    if( date2 > date30Days) 
        return false; 
    else 
        return true; 
    } 
}

Test Class [Test method - verifyDate.DateWithin30Days(dt1, dt2) throwing error]

Error Method is not visible: verifyDate.DateWithin30Days(Date, Date)

@IsTest
public class TestVerifyDate {   
    @IsTest 
    static void checkDatesTest(){
        Date Dt1 = Date.today();
        Date Dt2 = Date.today()+20;
        Date Dt = verifyDate.CheckDates(Dt1, Dt2);
        system.assertEquals(Dt2, Dt);
        Dt2      = Date.today()+40;

        Dt = verifyDate.CheckDates(Dt1, Dt2);
        system.assertEquals(Dt1.addMonths(1).toStartofMonth().addDays(-1),Dt);
    }

    @IsTest 
    static void DateWithin30DaysTest(){
        Date Dt1 = Date.today()+40;
        Date Dt2 = Date.today();
        Boolean Flip1 = verifyDate.DateWithin30Days(dt1, dt2);//Error
        system.assertEquals(False, Flip1);

        Dt1 = Date.today();
        Dt2 = Date.today()+90;
        Boolean Flip2 = verifyDate.DateWithin30Days(dt1, dt2);//Error
        system.assertEquals(False, Flip2);        
    }
}

can you help me to understand, how can I access the Private Apex Class method DateWithin30Days from Test Class Method DateWithin30DaysTest(). Please let me know what modification I need to do in Test Class method to achieve this?

Note: I have modified the Apex class for using @TestVisible Annotation and its working while using with Private method. But want to check whether the Private class can be access via Test class method itself.

  • 1
    hi @robertThomas you dont need to cover DateWithin30Days method separately. The CheckDates method calls DateWithin30Days method. So covering the CheckDates method will eventually cover DateWithin30Days method. Hence thats the challenge. You dont have to access private method. Because your public method has access to it. – snehakem Feb 21 '17 at 6:54
  • Your taking answers without understanding the point. You don't need access to the method directly from the test method to cover it. Since you keep asking that question I hope you can understand what I am trying to get across – Eric Feb 21 '17 at 6:56
2

An alternate approach is to just not annotate it and test it via the methods that use it.

It's private for a reason so why open it up to test methods.

You can create scenarios to test you logic for the methods that call it.

Use TestVisible sparingly and if that cannot be done question why it is private.

In your case just call verifyDate with different dates and assert the returned values are correct. Use different combinations to test the varying outcomes. Also test null dates to ensure error handling/protection is appropriate

Sometimes you have to use TestVisible like for a property that you need to set to alter behavior outside of the normal code flow. But if it can be tested within the normal code flow I would go that route first

Private methods can only be access from within the class they are defined. As such only the methods within that class use them. Test these methods is organic as you test the rest of the class

3

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.
Ref: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_testvisible.htm

@TestVisible private static Boolean DateWithin30Days(Date date1, Date date2){ }


Access Modifiers

By default, a method or variable is visible only to the Apex code within the defining class. You must explicitly specify a method or variable as public in order for it to be available to other classes in the same application namespace
You can change the level of visibility by using the access modifiers:

private This is the default, and means that the method or variable is accessible only within the Apex class in which it is defined. If you do not specify an access modifier, the method or variable is private.

public This means the method or variable can be used by any Apex in this application or namespace. This means the method or variable can be used by any Apex in this application or namespace.

Ref: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_access_modifiers.htm

  • 1
    But I can't edit my Apex class.. I am looking for something which can be done from Test Class. – Robert Thomas Feb 21 '17 at 6:45
  • Why can't you edit your Apex Class? – Nachiket Deshpande Feb 21 '17 at 6:49
  • I got it as a Challenge in Trailhead and they have given the Apex class like this and ask to write the Test class for it. SO that means I can't modify my Apex class. Is there any possiblity? trailhead.salesforce.com/trails/force_com_dev_beginner/modules/… – Robert Thomas Feb 21 '17 at 6:50
  • Then why you need to expose method as Test Visible its very straight forward you just need to pass parameters to CheckDates method! – Nachiket Deshpande Feb 21 '17 at 6:54
  • 1
    It says that you can't call private methods directly into test class unless you have annotated it with @TestVisible, if there are any private methods you need to call public methods which will call your private methods. – Nachiket Deshpande Feb 21 '17 at 7:02
1

Use the TestVisible annotation to allow test methods to access private or protected members of another class outside the test class.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_testvisible.htm

  • Is there anything that can be done from Test Class to access the Private Apex class methods without modifying the Apex Class methods to TestVisible. – Robert Thomas Feb 21 '17 at 6:47
  • You need to have @testVisible annotation in your apex class to access that private method in your test class. – Samir Feb 21 '17 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.