I'm trying to demonstrate the usage of System.runAs() to avoid mixed dml errors in unit tests. I know I've had to do some trickery when working with setup objects and then running tests. It is also documented. For example, I thought this test class would throw the error, but it isn't. Did something change in how tests run?
@isTest
private class RunAsTest {
@isTest static void mixedDml() {
Profile p = [select id from profile where name='Standard User'];
UserRole r = [Select id from userrole where name='COO'];
User u = new User(alias = 'standt', email='[email protected]',
emailencodingkey='UTF-8', lastname='Testing',
languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, userroleid = r.Id,
timezonesidkey='America/Los_Angeles',
username='[email protected]');
insert u;
Account a = (Account) TestFactory.createSObject(new Account());
insert a;
//System.runAs(u) {
a.Name = 'New Name';
update a;
//}
}
}