+ Start a Discussion
divya1234divya1234 

Looking for helper test class to create test user data










here is the quick background:- i wrote a validation rule to restrict the picklist value on user object but there is losts of test claases are getting failed. i choose teh approach to create a helper class to create user test data then i can call. i tried to write the test class not sure it will work please help me with the good approach . how can i can mmy helper class into my test class
global class TestClassHelper
{    
     public User CreateUser()
    {
        //Get a profile id from the data base to assign to our user in this case make them a sys admin
        Profile p = [select id from profile where name='System Administrator'];
           
           //Create a new sys admin user and do an insert  
        User u = new User(alias = 'standt', email='noEmail@testorg.com',
                          emailencodingkey='UTF-8', lastname='Testo', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id,
                          timezonesidkey='America/Los_Angeles', username='MrTestUser@testorg.com',Resource_Type__c='Employee');
                          
        User u1 = [Select u.Profile.Name, u.ProfileId, u.Name, u.Id From User u where u.Profile.Name = 'System Administrator' limit 1];
                          
        return u;
    }
    
    public User CreateUser(string userType)
    {
        //Get a profil id from the data base to assign to our user in this case make them a sys admin
        Profile p = [select id from profile where name=:userType];
           
           //Create a new sys admin user and do an insert  
        User ur = new User(alias = 'standt', email='noEmail@testorg.com',
                          emailencodingkey='UTF-8', lastname='Testo', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id,
                          timezonesidkey='America/Los_Angeles', username='MrTestUser@testorg.com',Resource_Type__c='Employee');
           return ur;
    }
}
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for test Factory
1) http://amitsalesforce.blogspot.com/2017/01/test-utility-classes-testdatafactory.html

Update your test helper like below
@isTest
Public class TestClassHelper
{    
     public User CreateUser()
    {
        //Get a profile id from the data base to assign to our user in this case make them a sys admin
        Profile p = [select id from profile where name='System Administrator'];
           
           //Create a new sys admin user and do an insert  
        User u = new User(alias = 'standt', email='noEmail@testorg.com',
                          emailencodingkey='UTF-8', lastname='Testo', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id,
                          timezonesidkey='America/Los_Angeles', username='MrTestUser@testorg.com',Resource_Type__c='Employee');
                          
        User u1 = [Select u.Profile.Name, u.ProfileId, u.Name, u.Id From User u where u.Profile.Name = 'System Administrator' limit 1];
                          
        return u;
    }
    
    public User CreateUser(string userType)
    {
        //Get a profil id from the data base to assign to our user in this case make them a sys admin
        Profile p = [select id from profile where name=:userType];
           
           //Create a new sys admin user and do an insert  
        User ur = new User(alias = 'standt', email='noEmail@testorg.com',
                          emailencodingkey='UTF-8', lastname='Testo', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id,
                          timezonesidkey='America/Los_Angeles', username='MrTestUser@testorg.com',Resource_Type__c='Employee');
           return ur;
    }
}

Let us know if this will help you
divya1234divya1234
 i am getting an error saying variable u does not exist, when i am trying to call helper class in my class
public static testMethod void testAccountPhoneFax(){
        TestHelper.createCustomSettings();
        setupData();
       TestClassHelper.CreateUser();
        System.runAs(u)
        {
            System.debug('Current User: ' + UserInfo.getUserName());
            System.debug('Current Profile: ' + UserInfo.getProfileId());
        }
divya1234divya1234
how can i call 
    public User CreateUser(string userType) method in my test class