3

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='standarduser@testorg.com', 
                emailencodingkey='UTF-8', lastname='Testing', 
                languagelocalekey='en_US', 
                localesidkey='en_US', profileid = p.Id, userroleid = r.Id,
                timezonesidkey='America/Los_Angeles', 
                username='standarduser3234234@testorg.com');
        insert u;
        Account a = (Account) TestFactory.createSObject(new Account());
        insert a;

        //System.runAs(u) {
            a.Name = 'New Name';
            update a;
        //}
    }
}
4

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='standarduser@testorg.com', 
                emailencodingkey='UTF-8', lastname='Testing', 
                languagelocalekey='en_US', 
                localesidkey='en_US', profileid = p.Id,
                timezonesidkey='America/Los_Angeles', 
                username='standarduser3234234@testorg.com');
        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. – Daniel Hoechst May 21 '14 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: [] – DerekLansing May 21 '14 at 5:52
  • Just to double-check you're using api version 30, right? – DerekLansing May 21 '14 at 5:53
  • Well, now it is throwing the error. Maybe working on it at midnight had something to do with it. – Daniel Hoechst May 21 '14 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. – Daniel Hoechst May 22 '14 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.