Skip to main content
Added Note Section
Source Link
Robert Thomas
  • 1.8k
  • 4
  • 34
  • 57

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.

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.

Source Link
Robert Thomas
  • 1.8k
  • 4
  • 34
  • 57

Accessing Apex Class'es Private methods from Test Methods

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?