Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

When should I use the Testvisible Annotation?

Asked by Aalap Prabhakaran Apr 17, 2023 1.4K views 2 answers
Share

About this question

I understand that @TestVisible is used to allow Unit Tests access to private and protected methods.

What I was wondering is, when is it appropriate to use this annotation?

For example, I have a list generated in a wrapper class that allows selection and processing of Users as below:

public class uUser {
    public User user {
      get;
      set;
    }
    public Boolean selected {
      get;
      set;
    }
    public uUser(User u) {
      user = u;
      selected = false;
    }
  }
I then used the following Apex to select all or deselect all these Users:
private Boolean isAllUsersSelected {
  get {
    if (isAllUsersSelected == null) {
      isAllUsersSelected = false;
    }
    return isAllUsersSelected;
  }
  set;
}
public PageReference selectAllUsers() {
  if (isAllUsersSelected == false) {
    isAllUsersSelected = true;
    for (uUser user : userList) {
      user.selected = true;
    }
  }
  else {
    isAllUsersSelected = false;
    for (uUser user : userList) {
      user.selected = false;
    }
  }
  return null;
}

Because this depends a lot on whether the isAllUsersSelected variable is true or false, I wondered whether or not it is a good idea and appropriate to use a @TestVisible method, given its access modifier is private?

Your answer

2 Answers

Sheri Gaskell Latest answer

Answered on Jan 12, 2026

Hello @ skeletal shenanigans, in your case, since the isAllUsersSelected property is crucial in determining the behavior of your selectAllUsers method, making it @TestVisible could be beneficial for unit testing:

@TestVisible

private Boolean isAllUsersSelected {

  get {

    if (isAllUsersSelected == null) {

      isAllUsersSelected = false;

    }

    return isAllUsersSelected;

  }

  set;

}

Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.