We typically validate our source control nightly and run all tests. This is essentially a validate only deployment running all tests against production. All tests pass.
However, if we were to run tests through Apex Test Execution or through Dev Console/IDE in production, there are several test methods that fail with a MIXED_DML_OPERATION
.
The exception is correct, we can update the methods to use System.runas()
and get the test methods working correctly, but I'm more interested in whether this is expected? I saw this previous question about differences when running tests in Mavenmate, but this is different with running a test method on its own vs. in a deployment/validation.
I know more CPU time is allocated for tests running in a deployment context vs. running the test on its own so I suspect this might be another thing Salesforce treats differently in deployments.
However, I can understand the difference in CPU time whereas I can't quite understand this behavior.
I also tested in a sandbox using the code example in their documentation. I removed the System.runAs()
so it should fail. This test fails in Dev Console, but passes in a deployment/validation.
@isTest
private class MixedDML {
static testMethod void mixedDMLExample() {
User u;
Account a;
User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
// removed System.runas() to throw mixed_dml
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
UserRole r = [SELECT Id FROM UserRole WHERE Id = 'Id here'];
u = new User(alias = 'jsmith', email='[email protected]',
emailencodingkey='UTF-8', lastname='Smith',
languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, userroleid = r.Id,
timezonesidkey='America/Los_Angeles',
username='[email protected]');
insert u;
a = new Account(name='Acmetest123');
insert a;
}
}
Edit: I've created a support case as well