9

Can any one help me on this Error as **Variable is not visible** in a test class.Any help very much appreciated.

Code :

 public with sharing class InvoiceController {
 List<wrapperClass> appointmentList {get;set;}//These three lines are not 
 List<wrapperClass> appointmentList1 {get;set;} //covered in test class
 public list<string> address{get;set;}//

}

Test Class :

InvoiceController icwc = new InvoiceController(sc);
    icwc.appointmentList=icwc.getappointmentList();
    icwc.appointmentList1=icwc.getappointmentList1();

The system throws an Error as :

Variable is not visible: appointmentList 
 icwc.appointmentList=icwc.getappointmentList();

Similarly for the other

Variable is not visible: appointmentList1 
 icwc.appointmentList1=icwc.getappointmentList1();
3
  • The properties you are trying to access do not have an access modifier. Commented Apr 15, 2015 at 22:15
  • 3
    Yep. Default access modifier is private.
    – pchittum
    Commented Apr 15, 2015 at 23:09
  • I did not know that. That's good to know. Makes sense though. I prefer to add the private keyword to be more explicit. Commented Apr 15, 2015 at 23:27

2 Answers 2

8

That's because you did not label your properties as public (you don't have an access modifier at all) in your class. I am going under the assumption that you wanted these List to be accessible publicly.

If you do this:

public List<wrapperClass> AppointmentList { get;set; }
public List<wrapperClass> AppointmentList1 { get;set; }

they will be accessible in your test class.

If it is not meant to be visible i.e. private, you should specify it as such with the keyword private and you can use the @TestVisible annotation as suggested. However this should be a last resort!

Before you do that however, you should do the following:

  • Try to figure out if there is a way to populate it in your test method
  • See if the property is actually necessary
  • Find another way to initialize the property in your class so you code is actually testable

If you cannot find a way to populate your private properties, after you have exhausted all options, then you can use the @TestVisible annotation.

Here would be an example:

@TestVisible private List<wrapperClass> AppointmentList { get;set; }
@TestVisible private List<wrapperClass> AppointmentList1 { get;set; }
4

Use TestVisible annotations for private class variables to be accessed in test classes. Please refer https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_testvisible.htm

4
  • 2
    This to me is not a valid answer. This is simply a band-aid to the actual issue. Using the @TestVisible annotation should only be a last resort, which in this case, it's not. It is simply an issue with not adding an access modifier - adding public. Commented Apr 15, 2015 at 22:46
  • 1
    @ProgrammableMedley I would disagree. A property should not be made public simply because it is to be accessed in a test class. This is the core reason for providing "@TestVisible" annotation.
    – Anshul
    Commented Apr 16, 2015 at 4:10
  • That is not what I'm implying at all. Simply adding the public keyword to make it testable is not what I'm getting at, nor would I ever endorse. I believe the OP thought his properties were public without the access modifier. Also, the @TestVisible annotation was provided so you can access variable in test, when you could not properly recreate the actual test scenario in your test class. What you are suggesting is simply adding the @TestVisible annotation without even attempting to make your code testable. Commented Apr 16, 2015 at 5:56
  • Oh , got it. Yes I agree, would have been better to cover basics first. I was assuming that this would be obvious for the developer.
    – Anshul
    Commented Apr 16, 2015 at 13:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .