2

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?

4

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'
    );
}
| improve this answer | |

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.