function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MattMet86MattMet86 

Test Class Help - Mixed DML Error - User, Account, Public Group

I am getting the following error when I run my test class
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: User: []

Here is my complete test class. I have tried every variation of the test.starttest and test.stoptest I can think of but i can't get past the error. 
 
@isTest
//Created by Matt - March 2016
//Designed for the Acorn Release
//

public class Test_Session_and_Session_Benefit {
    @testSetup static void setupTestData(){
        
        Test.startTest(); 
        List<User>testUsers = new List<User>();
        
        //Create Admin User
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
        testUsers.add(new User(Alias = 'SysAdmin', Email='admin@bcinsourcing.com', 
                          EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', TimeZoneSidKey='America/Los_Angeles', ProfileId = p.Id, 
                          UserName='admin@bcinsourcing.com'));

         //Create Manager
        Profile pM = [SELECT Id FROM Profile WHERE Name='BCS Manager']; 
        testUsers.add(new User(Alias = 'TSTMGR', Email='testmanager@bcinsourcing.com', 
            EmailEncodingKey='UTF-8', LastName='TSTMGR', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', TimeZoneSidKey='America/Los_Angeles', ProfileId = pM.Id, 
            UserName='testmanager@bcinsourcing.com'));

        //Create Standard User
        Profile pS = [SELECT Id FROM Profile WHERE Name='Chatter Only - BCS Standard']; 
        testUsers.add(new User(Alias = 'TSTBCS', Email='TSTBCS@bcinsourcing.com', 
            EmailEncodingKey='UTF-8', LastName='TSTBCS', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', TimeZoneSidKey='America/Los_Angeles', ProfileId = pS.Id, 
            UserName='TSTBCS@bcinsourcing.com'));
        
        	Insert testUsers;
                 
        //Create Accounts
        List<Account> accounts = new List<Account>();
        for(Integer i=0;i < 2;i++){
            accounts.add(new Account(
                Name = 'Test Acc' +i,
                Account_Code__c = 'AC' +i,
                Type = 'Client',
                Niche__c = 'Services'
            ));
        }
        
        	insert accounts; 
        Test.stopTest();
 
        //Create Public Group
        List<Group> pubGroup = new List<Group>();
        for(Account acc:accounts){
            pubGroup.add(new Group(
                Name             = acc.Name,
                DeveloperName    = acc.Name,
                Type             = 'Regular'
             ));
        }
        
        	insert pubGroup;
        
        //Create Carriers
        List<Account> carriers = new List<Account>();
        for(Integer i=0;i < 2;i++){
            carriers.add(new Account(
                Name = 'Test Carrier' +i,
                Account_Code__c = 'CA' +i,
                Type = 'Carrier'
            ));
        }
        insert carriers;
        
        //Create Hub
        List<BCS__c> theHub = new List<BCS__c>();
        for(Integer i=0;i < 1;i++){
            theHub.add(new BCS__c(
                Name = testUsers.get(0).LastName,
                DOH__c = Date.today().addDays(2),
                User__c = testUsers.get(0).Id
            ));
        }
        insert theHub;
        
        //Create Benefit Masters
        List<Benefit_Master__c> benMasters = new List<Benefit_Master__c>();
        for(Account car:carriers){
            for(Integer i=0;i < 2;i++){
                benMasters.add(new Benefit_Master__c(
                    Name = 'BenMaster'+i,
                    Carrier__c = car.Id
                ));
            }
        }
        insert benMasters;
        
        //Create Account Benefits
        List<Account_Benefit__c> AccBenefit = new List<Account_Benefit__c>();
        for(Benefit_Master__c master:benMasters){
            for(Integer i=0;i < 2;i++){
                AccBenefit.add(new Account_Benefit__c(
                    Name = master.Name,
                    Benefits_Master__c = master.id,
                    Account__c = accounts.get(i).Id,
                    Show_Elimination_Period__c   = false,
                    Show_Volume__c               = false
                ));
            }
        }
        insert AccBenefit;
        
        //Create Employees
        List<Employees__c> employee = new List<Employees__c>();
        for(Account acc:accounts){
            for(Integer i=0;i < 10;i++){
                employee.add(new Employees__c(
                    Name = acc.Account_Code__c + i, //Creates a HBS ID like AC11 and AC12
                    First_Name__c = 'First'+i,
                    Last_Name__c = 'Last'+i,
                    Account__c = acc.Id,
                    Review_End_Date__c = Date.today(),
                    DOH__c = Date.today(),
                    PayCy__c = 'Biweekly'
                ));
            }
        }
        insert employee;
    }
    
    
    //Create Acorn Sessions  
    static testMethod void testCreateSessions(){
        
        ....subsequent code redacted....
}

Any ideas? 
 
@Karanraj@Karanraj
Some sObjects require that you perform DML operations on only one type per transaction. For example, you cannot insert an account, then insert a user or a group member in the same transaction. The following sObjects cannot be used together in a transaction:
* Group1 
* GroupMember
* QueueSObject
* User2 
* UserRole 
* UserTerritory
* Territory
The primary exception to this is when you are using the runAs method in a test.

So use run as in your test class to avoid this issue, insert users and groups in different user context and then insert the account and other sobject with the newly inserted user record. Check this link for the samples - http://stackoverflow.com/questions/2387475/how-to-avoid-mixed-dml-operation-error-in-salesforce-tests-that-create-users
Mahesh DMahesh D
You can go through these posts:

http://salesforce.stackexchange.com/questions/13318/error-mixed-dml-operation-on-setup-and-non-setup-objects
https://developer.salesforce.com/forums/?id=906F00000008wAZIAY

You may need to use runAs method.

Regards,
Mahesh