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:
I found that (when the batches execute during Test.stopTest):
- 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)
- 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.