+ Start a Discussion
JN22JN22 

Create Dummy Users for Testing

Hello,

I have a test class that creates and Account (below).  In this class, I have included actual User IDs, which is not ideal, especially when users are de-activated.  How do I create dummy User IDs with specific profiles that I can use in this Class rather than the actual IDs?


public class TestStatusAcctCreate {

        // create and insert a new Account record that has an arbitrary value for Type and users assigned to the 3 custom fields.
    public static Account createAcct(Integer i){ 
        Account acct = new Account();
            acct.Name = 'Test' + i;
            acct.Type= 'Employer';
            acct.Client_Advisor__c = '00570000001LlBD';
            acct.Industry_Manager__c = '00570000001JKVQ';
            acct.Market_Director__c = '00570000001r5xs';
            
        return acct;
        
    }
}


Best Answer chosen by JN22
Daniel B ProbertDaniel B Probert
public class TestStatusAcctCreate {
    public static Account createAcct(Integer i){
        Profile p =[SELECT ID FROM Profile Where Name='Standard User'];
        User U1 = new user(alias='demo1',LastName='Demo1',Username'demo1@demopods.com');
        insert u1;
User u2 = new user(alias='demo2',LastName='Demo1',Username'demo2@demopods.com');
        insert u2; 
        User u3 = new user(alias='demo3',LastName='Demo1',Username'demo3@demopods.com');
        insert u3;
        Account acct = new Account();
            acct.Name = 'Test' + i;
            acct.Type= 'Employer';
            acct.Client_Advisor__c = u1.id;
            acct.Industry_Manager__c = u2.id;
            acct.Market_Director__c = u3.id;
        return acct;
    }
}
something like that should do the job.

thanks
dan

All Answers

Daniel B ProbertDaniel B Probert
use this you'll need to add one more user..

Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u1 = new User(Alias = 'standt1',Country='United Kingdom',Email='demo1@randomdemodomain.com',EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',LocaleSidKey='en_US',ProfileId = p.Id,TimeZoneSidKey='America/Los_Angeles', UserName='demo1@andomdemodomain.com');
insert u1;
User u2 = new User(Alias = 'standt2',Country='United Kingdom',Email='demo2@randomdemodomain.com',EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',LocaleSidKey='en_US', ProfileId = p.Id,TimeZoneSidKey='America/Los_Angeles', UserName='demo2@andomdemodomain.com');
insert u2;

if this helps mark as resolved :)
JN22JN22
Thanks.  Do I create a separate class using that code and then call it into my TestStatusAcctCreate class or do I incorporate the code directly into my TestStatusAcctCreate class?  I'm confused how to do either for a user.  Thanks.
Daniel B ProbertDaniel B Probert
public class TestStatusAcctCreate {
    public static Account createAcct(Integer i){
        Profile p =[SELECT ID FROM Profile Where Name='Standard User'];
        User U1 = new user(alias='demo1',LastName='Demo1',Username'demo1@demopods.com');
        insert u1;
User u2 = new user(alias='demo2',LastName='Demo1',Username'demo2@demopods.com');
        insert u2; 
        User u3 = new user(alias='demo3',LastName='Demo1',Username'demo3@demopods.com');
        insert u3;
        Account acct = new Account();
            acct.Name = 'Test' + i;
            acct.Type= 'Employer';
            acct.Client_Advisor__c = u1.id;
            acct.Industry_Manager__c = u2.id;
            acct.Market_Director__c = u3.id;
        return acct;
    }
}
something like that should do the job.

thanks
dan
This was selected as the best answer
JN22JN22
Thanks, that did the trick!