Test failure: mixed DML operation
Detection
- String:
MIXED_DML_OPERATION
- Applies to: Apex test failures
Examples
System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): User, original object: Account: []
Resolution
The test inserts or updates a setup object (User, Group, PermissionSetAssignment...) and another object in the same transaction.
- Wrap the DML of the setup objects in System.runAs(new User(Id = UserInfo.getUserId())) { ... }
- Or create them in a @TestSetup method