0

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');

        system.debug(Integer.valueof(Label.Secure_provisioning_90_days_notification.trim())*-1+'###'+d);

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

        system.debug('###query##'+query);

        selectedUsers = Database.query(query);

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



        for(User record: selectedUsers) {

            record.IsActive = false;

            UserList.add(record);



            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});

            email.setTargetObjectId(record.id);

            email.setSaveAsActivity(false);

            String body = getHTMLBody90days(record);

            email.setHtmlBody(body);

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

            email.setUseSignature(false);

            emailsList.add(email);

        }

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 '17 at 7:11
  • Did n't work either. – Pankaz Aug 15 '17 at 7:14
  • Please check my answer. – jagmohan Aug 15 '17 at 7:20
2

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.

Eg.

public class MyClass {
    @TestVisible
    private DateTime myDateTime;

    ...
}

// Test class
@IsTest(SeeAllData=false)
private class MyClassTests {

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

Hope this helps.

| improve this answer | |
1

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.

| improve this answer | |
0

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

| improve this answer | |
  • Sorry, that was a typo. I have corrected it. – Pankaz Aug 15 '17 at 6:59

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.