
@isTest
private class MixedDML {
static testMethod void mixedDMLExample() {
User u;
Account a;
User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
// Insert account as current user
System.runAs (thisUser) {
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
UserRole r = [SELECT Id FROM UserRole WHERE Name='COO'];
u = new User(alias = 'jsmith', email='jsmith@acme.com',
emailencodingkey='UTF-8', lastname='Smith',
languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, userroleid = r.Id,
timezonesidkey='America/Los_Angeles',
username='jsmith@acme.com');
insert u;
a = new Account(name='Acme');
insert a;
}
}
}
As per the link, this code should throw error if i remove the block
System.runAs() but it is working fine.Am I doing something wrong??I am just running my test class in mavensmatehi Deter,I understand that what r u trying for. Actually System.runAs() used when we need a customer portal user for test class , means that in this case u have no user and u create a temp user for noly test class.like : @isTestprivate class MixedDML { static testMethod void mixedDMLExample() { //Create Account Account accObj = new Account(name='Test'); insert accObj; //Create Contact Contact con = new contact(lastname='test',email='test@gmail.com',accountid=accObj.id); insert con; Profile p = [SELECT Id FROM Profile WHERE Name=''];//Profile name of customer portal Database.DMLOptions dmo = new Database.DMLOptions(); dmo.EmailHeader.triggerUserEmail = false; User u = new User(alias = 'standt', email=con.email, emailencodingkey='UTF-8', lastname=con.lastname, firstname=con.firstname, languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, contactId=con.Id, timezonesidkey='America/Los_Angeles', username=con.email); u.setOptions(dmo); insert u; System.runAs(u) { //Some work with this test user } }}In your code u are using real user so i think it will hrlpful.