4

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;
        //}
    }
}

2 Answers 2

5

I got a mixed dml exception with the following code.

@isTest
public class testTester {
    public static testmethod void testMixedDml() {

        Profile p = [select id from profile where name='Standard User'];

        User u = new User(alias = 'standt', email='[email protected]', 
                emailencodingkey='UTF-8', lastname='Testing', 
                languagelocalekey='en_US', 
                localesidkey='en_US', profileid = p.Id,
                timezonesidkey='America/Los_Angeles', 
                username='[email protected]');
        insert u;


        Group g = new Group();
        g.Name = 'Test Group';
        g.DeveloperName = 'TestGroup';
        insert g;

        GroupMember gm = new GroupMember();
        gm.GroupId = g.Id;
        gm.UserOrGroupId = u.Id;
        insert gm;


        Account a = new Account();
        a.Name = 'Test Account';
        insert a;
    }
}
6
  • Hmm, that still isn't giving me a mixed dml error. May 21, 2014 at 5:50
  • Error Message 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): Account, original object: GroupMember: [] May 21, 2014 at 5:52
  • Just to double-check you're using api version 30, right? May 21, 2014 at 5:53
  • Well, now it is throwing the error. Maybe working on it at midnight had something to do with it. May 21, 2014 at 12:57
  • This gets weirder. The code you posted doesn't throw an error when I run the test from MavensMate, but does throw an error in the web UI. May 22, 2014 at 3:38
1

Run the test using Developer Console or through the UI using Run Test. If you are using Eclipse or using the MavensMate Apex Test Runner UI the tests will pass.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .