I am facing Mixed DML error in the following case:
static testmethod void method1()
{
User u1 = DataFactory.createUser('testUser','[email protected]','permissionSetAPIname');
Obj1 rec1 = DataFactory.createrec1('Record1');
insert rec1;
Obj2 rec2 = DataFactory.createrec2('Rec2',rec1,system.today()-10, system.today()+10 );
insert rec2;
Obj3 rec3 = DataFactory.createrec3(rec2,'Tester',u1,'US');
insert Role;
test.starttest();
//testing code
test.stoptest();
}
However the following is working fine
static testmethod void method1()
{
User u1 = new User(Alias = 'testRole',
Email = '[email protected]',
EmailEncodingKey = 'UTF-8',
LastName = 'user',
FirstName = 'test',
LanguageLocaleKey = 'en_US',
LocaleSidKey = 'en_US',
ProfileId = [select Id from Profile where Name = 'Basic User' limit 1].Id,
TimeZoneSidKey = 'America/Chicago',
Username = 'stakeholder@test' + Math.round(Math.random() * 10000) + '.com',
UserRoleId = null,
Country = 'Great Britain');
insert u1;
Obj1 rec1 = DataFactory.createrec1('Record1');
insert rec1;
Obj2 rec2 = DataFactory.createrec2('Rec2',rec1,system.today()-10, system.today()+10 );
insert rec2;
Obj3 rec3 = DataFactory.createrec3(rec2,'Tester',u1,'US');
insert Role;
test.starttest();
//testing code
test.stoptest();
}
Please explain the difference. As my understanding is that transaction remains the same in both the cases.