function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
divya1234divya1234 

Test Classes are getting failed due to validation rule on user object. looking for helper class


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 my 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;
    }
}


how can i call both method in my test class.

user u = TestClassHelper.CreateUser();?

i am not sure how i can call  public User CreateUser(string userType)
user u = TestClassHelper(---) ,what should be argument ?..this is my helper class do i need 2  methods?



 
Gokula KrishnanGokula Krishnan
Hi Divya,

Try this,

TestClassHelper tcls = new TestClassHelper();
user u = tcls.CreateUser();
u = tcl.CreateUser('UserName');//input any UserName

Thanks,