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?

1 Answer 1

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

You must log in to answer this question.

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