How do I insert a User which I can use for testing?

77 views Asked by At

I need writing some apex test code, where I want to mock a user which I then via the test has to find.

I seem to have some problems creating one though?

So far I understand I need an account, Contact and a User which the contact references to.

But I seem to run into problems when I try to insert the user:

Here is the code so far

public static Auth.UserData createUserDataWithProfile() {
    Account acc = new Account(
      name = 'Test Account' + '1',
      BillingStreet = 'Somestrasse 25',
      BillingCity = 'Edinburg',
      BillingPostalCode = '123442',
      BillingCountry = 'Germany'
    );
    insert acc;

    Contact c = new Contact(LastName = 'Test', Email = '[email protected]', AccountId = acc.Id);

    // Insert the contact
   insert c;        
       Profile p = [SELECT Id FROM Profile WHERE Name = 'customname'];

// Create a new 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]',
  UserRoleId = '00EVe000000PD26MAG'
);

// Insert the user
insert u; // here it fails
}

The error message I get is

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: []

Which I don't get, User is a setup object, but its only created after account and contact - so what is the problem, why am I not allowed to this?

1

There are 1 answers

0
Meenakshi Govindasamy On

To resolve this issue, you need to split your code into separate transactions. You can use the 'System.runAs' method to perform the user creation part in a separate context. By using System.runAs, you're executing the user creation part with the context of the user who is running the test, allowing you to perform DML operations on setup objects without violating the MIXED_DML_OPERATION restriction. `

public static void testUserDataCreation() {
             // Insert Account and Contact
            Account acc = new Account(
                Name = 'Test Account' + '1',
                BillingStreet = 'Somestrasse 25',
                BillingCity = 'Edinburgh',
                BillingPostalCode = '123442',
                BillingCountry = 'Germany'
            );
            insert acc;
    
            Contact c = new Contact(LastName = 'Test', Email = '[email protected]', AccountId = acc.Id);
            insert c;
        
            // Perform User creation in a separate context
            System.runAs(new User(Id = UserInfo.getUserId())) {
                Profile p = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
                Userrole roleidval = [SELECT Id, Name
                                        FROM UserRole
                                        WHERE Name='CEO'];
                // Create a new 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]',
                    UserRoleId = roleidval.Id
                );
    
                // Insert the user
                insert u;
            }
        
              }

`