I'm trying to write a unit test code for the below method but despite using @TestVisible annotation also I'm not able to access the local variables inside my test class. Is there anything I'm missing.

public void inactiveUsers(){

       // List<String> excludedUserList = getExcludedUsers();

        List<user> UserList = new List<user>();       

         List<Messaging.SingleEmailMessage> emailsList = new List<Messaging.SingleEmailMessage>();

       @TestVisible DateTime dateBefore_90days = Date.today().addDays(Integer.valueof(Label.Secure_provisioning_90_days_notification.trim())*-1);

         @TestVisible string d = dateBefore_90days.format('yyyy-MM-dd');


        string query = 'SELECT Id,email,username,lastlogindate FROM User WHERE IsActive = TRUE and DAY_ONLY(convertTimezone(LastLoginDate)) <= '+ d;


        selectedUsers = Database.query(query);

        system.debug('Secure_provisioning_90days_notification list### '+selectedUsers);

        for(User record: selectedUsers) {

            record.IsActive = false;


            Messaging.Singleemailmessage email = new Messaging.Singleemailmessage();

            //  email.setSenderDisplayName('Optum Inactive User Notification');

            email.setToAddresses(new String[]{record.email});

            email.setCCAddresses( new String[]{Label.Secure_provisioning_cc_email_address});



            String body = getHTMLBody90days(record);


            email.setSubject('Your Access has been revoked');




here is my test code.

static testMethod void myTest() {
        DeactivateInactiveUsers.dateBefore_90days = Date.today(); // getting error: Variable does not exist: dateBefore_90days
        DeactivateInactiveUsers.d = 'some value';  


Any help would be appreciated.

  • 1
    You are trying to access a local variable declared within a method. The correct way is to declare a class member variable and annotate it as test visible and you should be able to make it work.
    – jagmohan
    Aug 15, 2017 at 7:11
  • Did n't work either.
    – Pankaz
    Aug 15, 2017 at 7:14
  • Please check my answer.
    – jagmohan
    Aug 15, 2017 at 7:20

3 Answers 3


You have declared local variables in inactiveUsers() method which you are trying to access in your test class. This is not possible.

The correct way is to declare the variables at class level and annotate them with @TestVisible. Also, you need to instantiate an object of the class your are trying to test if the variable is not static.


public class MyClass {
    private DateTime myDateTime;


// Test class
private class MyClassTests {

    static void shouldTest(){
        MyClass obj = new MyClass();
        obj.myDateTime = DateTime.now();

Hope this helps.


The appropriate way to test a method is to call it and assert the expected public results which in your case is to send emails that have specific content. Tying your tests to internal detail in a method makes it hard to make any future changes to the design.

The sending email case is a bit hard to test but this answer explains one technique.


There is no such variable "dateBefore_60days" in your actual class. It is "dateBefore_90days".

  • Sorry, that was a typo. I have corrected it.
    – Pankaz
    Aug 15, 2017 at 6:59

You must log in to answer this question.

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