Ensure that the runAs user applies when running a batch in a test - Ideas - Salesforce Trailblazer Community
Trailblazer Community

All Ideas

Idea Details

Post an Idea
50  Points
Idea has been posted. Give it an upvote or downvote.

Ensure that the runAs user applies when running a batch in a test

Development (Apex, LWC & VF)

It is not possible to create two batch processes run as different users in a test. I need to be able to verify that my batch processes are not both run by the same person but can be successfully executed by different people, and I can't test this.

Creating a test like:

System.runAs(someUser) {

System.runAs(someOtherUser) {


I found that (when the batches execute during Test.stopTest):
  1. They are run apparently in parallel (the AsyncApexJobs details accessed during the start method invocations seem to suggest that both get started before either is finished)
  2. They are both run using the user that is in place when Test.stopTest is called
The first point is great. The second point breaks my test.

The "idea" is to have test execution of batches (or other async code) correctly remember the contextual user at the time they are initialized and for that user to be applied during execution.

PS: IMHO this is a bug, not a feature, so should be handled via a support case. However, I cannot raise those apparently. See https://developer.salesforce.com/forums/ForumsMain?id=9060G0000005QdF for details.

Merge Idea · Flag

  • Upvotes
  • Downvotes



from AppExchange


No results found.

Help us to keep IdeaExchange clean by pointing out overlapping ideas. We'll investigate your suggestion and merge the ideas if it makes sense.



Thanks for your merge suggestion. We will review it shortly and merge the ideas if applicable.

Salesforce takes abuse situations very seriously. Examples of abuse include but are not limited to posting of offensive language or fraudulent statements. To help us process your request as quickly as possible, please fill out the form below describing the situation. For privacy and security reasons, the final outcome of an abuse case may not be revealed to the person who reported it.


Thank you for your feedback. We take abuse seriously and will investigate this issue and take appropriate action.