0

I'm new to Salesforce and I need set userId variable Id userId = UserInfo.getUserId() to null in order to test it. How can I do that?

3
  • 1
    You cannot set system variables, however, you can set Id userId = null; directly in your test and do not use UserInfo.getUserId(). Nov 19, 2019 at 13:48
  • Welcome to SFSE! Please take the tour and read How to Ask. Asking the same question multiple times is discouraged. I closed your other question as a duplicate because it was the same in substance as this one. Marking an answer as "accepted" is generally taken to mean that your problem has been solved, and discourages other people from adding their own answers. Generally, you can add comments to ask for additional information, though there's a fine line between "help me understand your answer", and asking something that should be a separate question.
    – Derek F
    Nov 19, 2019 at 14:54
  • Your code should never have to compare the result of UserInfo.getUserId() to null hence there's no need to test for it in a testmethod.
    – cropredy
    Nov 20, 2019 at 2:18

2 Answers 2

0

The answer to this is complicated, as commented by @mariia-illarionova you can not directly change the value of UserInfo.getUserId() thats because its a readonly property learn about that here (getter only). If you are serious about getting this done you would need to create a wrapper method to get UserInfo.getUserId(). Second you will need to inject a mock version of the method to the class from your test. Most people use Interfaces but there are other ways Interfaces are just the way to go in my opinion. Let me show you an example.


public interface UserOverrides {
    string getUserInfo();
}

public class UserWrapper implements UserOverrides{
    public string getUserInfo(){
        return UserInfo.getUserId();
    }
}

public class UseUserWrapper{
    private UserOverrides userOverride {get;set;}

    public UseUserWrapper(UserOverrides userOverride){
        this.userOverride = userOverride;
    }

    public void someAction(){
        System.Debug(this.userOverride.getUserInfo());
    }
}

@isTest
public class testUserWrapper{

    @isTest
    public testSomething(){
        UseUserWrapper useUserWrapper = new UseUserWrapper(new UserOverridesMock());
        useUserWrapper.someAction();
    }

    public class UserOverridesMock implements UserOverrides{
        public string getUserInfo(){
            return null;
        }
    }
}

As you can see it is quite the complicated answer, but dependency injection is a powerful tool especially for when it comes time to write unit tests. You can see from the code example that instead of using the UserWrapper class I am using a different implementation of UserOverrides in my test and passing it into UseUserWrapper. You can read about Interfaces here

1

You can't change the behavior of UserInfo.getUserId() method, it always returns Id of the user who started the context execution.

However, you could set it to null - Id userId = null;.

If your logic depends on the Id and it could be null and you need to test it - just declare userId as a variable of a class and make it @TestVisible, then set it to null during a test.

You must log in to answer this question.

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