Ask Search:
David GunnDavid Gunn 

What user do test methods run as?

During deployment to production, when test methods run what user do they run as?
Best Answer chosen by David Gunn
Swayam ChoukseySwayam Chouksey
Hi,

Generally, all Apex code runs in system mode, where the permissions and record sharing of the current user are not taken into account. The system method runAs enables you to write test methods that change the user context to an existing user or a new user so that the user’s record sharing is enforced. The runAs method doesn’t enforce user permissions or field-level permissions, only record sharing.

You can use runAs only in test methods. The original system context is started again after all runAs test methods complete.

If you had use runAs in any test methods, then they will run with that user otherwise always run in system mode

Hope this helps !!

--
Thanks,
Swayam

 

All Answers

Swayam ChoukseySwayam Chouksey
Hi,

Generally, all Apex code runs in system mode, where the permissions and record sharing of the current user are not taken into account. The system method runAs enables you to write test methods that change the user context to an existing user or a new user so that the user’s record sharing is enforced. The runAs method doesn’t enforce user permissions or field-level permissions, only record sharing.

You can use runAs only in test methods. The original system context is started again after all runAs test methods complete.

If you had use runAs in any test methods, then they will run with that user otherwise always run in system mode

Hope this helps !!

--
Thanks,
Swayam

 
This was selected as the best answer
Vinay ChaturvediVinay Chaturvedi
I would you to read these aerticles as well :
For more insights,Please check here:
Apex Testing with RunAs and Using the Run as method.

Generally, all Apex code runs in system mode, and the permissions and record sharing of the current user are not taken into account. The system method runAs enables you to write test methods that change either the user contexts to an existing user or a new user. When running as a user, all of that user's record sharing is then enforced. You can only use runAs in a test method. The original system context is started again after all runAs test methods complete.

The following items use the permissions granted by the user specified with runAs running as a specific user:
Dynamic Apex
Methods using with sharing or without sharing
Shared records
The original permissions are reset after runAs completes.
The runAs method ignores user license limits. You can create new users with runAs even if your organization has no additional user licenses.