Is there any way to set set salesforce userid from apex test class

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












Is there any way to set set salesforce userid from apex test class.



For Example:



SetupTest st = new SetupTest(); 
Account billing = st.Account('Billing');
billing.Name = 'Test_BillingAccount';
billing.Atlas_Id__c = 'a_' + billing.Name;
insert billing;

PageReference pageRef = Page.CreateSalesOrder;
pageRef.getParameters().put('id',string.valueOf(billing.Id));
Test.setCurrentPage(pageRef);


In the above code am passing dynamic account id from test page, Is there a way to pass userid(userinfo.getuserid()) like this?










share|improve this question





























    up vote
    2
    down vote

    favorite












    Is there any way to set set salesforce userid from apex test class.



    For Example:



    SetupTest st = new SetupTest(); 
    Account billing = st.Account('Billing');
    billing.Name = 'Test_BillingAccount';
    billing.Atlas_Id__c = 'a_' + billing.Name;
    insert billing;

    PageReference pageRef = Page.CreateSalesOrder;
    pageRef.getParameters().put('id',string.valueOf(billing.Id));
    Test.setCurrentPage(pageRef);


    In the above code am passing dynamic account id from test page, Is there a way to pass userid(userinfo.getuserid()) like this?










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Is there any way to set set salesforce userid from apex test class.



      For Example:



      SetupTest st = new SetupTest(); 
      Account billing = st.Account('Billing');
      billing.Name = 'Test_BillingAccount';
      billing.Atlas_Id__c = 'a_' + billing.Name;
      insert billing;

      PageReference pageRef = Page.CreateSalesOrder;
      pageRef.getParameters().put('id',string.valueOf(billing.Id));
      Test.setCurrentPage(pageRef);


      In the above code am passing dynamic account id from test page, Is there a way to pass userid(userinfo.getuserid()) like this?










      share|improve this question















      Is there any way to set set salesforce userid from apex test class.



      For Example:



      SetupTest st = new SetupTest(); 
      Account billing = st.Account('Billing');
      billing.Name = 'Test_BillingAccount';
      billing.Atlas_Id__c = 'a_' + billing.Name;
      insert billing;

      PageReference pageRef = Page.CreateSalesOrder;
      pageRef.getParameters().put('id',string.valueOf(billing.Id));
      Test.setCurrentPage(pageRef);


      In the above code am passing dynamic account id from test page, Is there a way to pass userid(userinfo.getuserid()) like this?







      apex unit-test






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 hours ago









      Adrian Larson♦

      101k19107226




      101k19107226










      asked 2 hours ago









      user3214361

      173




      173




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote













          You are looking for the system.runAs method.



          User someOtherUser = new User(/*required fields*/);
          system.runAs(someOtherUser)

          system.assertEquals(
          someOtherUser.Id,
          UserInfo.getUserId(),
          'The running user should be changed'
          );






          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "459"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f233664%2fis-there-any-way-to-set-set-salesforce-userid-from-apex-test-class%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote













            You are looking for the system.runAs method.



            User someOtherUser = new User(/*required fields*/);
            system.runAs(someOtherUser)

            system.assertEquals(
            someOtherUser.Id,
            UserInfo.getUserId(),
            'The running user should be changed'
            );






            share|improve this answer
























              up vote
              4
              down vote













              You are looking for the system.runAs method.



              User someOtherUser = new User(/*required fields*/);
              system.runAs(someOtherUser)

              system.assertEquals(
              someOtherUser.Id,
              UserInfo.getUserId(),
              'The running user should be changed'
              );






              share|improve this answer






















                up vote
                4
                down vote










                up vote
                4
                down vote









                You are looking for the system.runAs method.



                User someOtherUser = new User(/*required fields*/);
                system.runAs(someOtherUser)

                system.assertEquals(
                someOtherUser.Id,
                UserInfo.getUserId(),
                'The running user should be changed'
                );






                share|improve this answer












                You are looking for the system.runAs method.



                User someOtherUser = new User(/*required fields*/);
                system.runAs(someOtherUser)

                system.assertEquals(
                someOtherUser.Id,
                UserInfo.getUserId(),
                'The running user should be changed'
                );







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 hours ago









                Adrian Larson♦

                101k19107226




                101k19107226



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f233664%2fis-there-any-way-to-set-set-salesforce-userid-from-apex-test-class%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery