0

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='[email protected]',
                          emailencodingkey='UTF-8', lastname='Testo', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id,
                          timezonesidkey='America/Los_Angeles', username='[email protected]',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='[email protected]',
                          emailencodingkey='UTF-8', lastname='Testo', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id,
                          timezonesidkey='America/Los_Angeles', username='[email protected]',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?

2
  • TestClassHelper HelperObject=new TestClassHelper(); HelperObject.CreateUser(); call this way .
    – NITHESH K
    Commented Dec 27, 2017 at 5:55
  • TestClassHelper HelperObject=new TestClassHelper(); HelperObject.CreateUser('userType');
    – NITHESH K
    Commented Dec 27, 2017 at 5:56

1 Answer 1

2

You do not need to repeat so much code in your two implementations. You can use overloading to apply DRY (Don't Repeat Yourself) principles:

public User createUser()
{
    return createUser('System Administrator');
}
public User createUser(String profileName)
{
    // implementation
}

Notice that I changed the parameter name to profileName, which is more descriptive of what type of input the method expects. Another more significant change I recommend is to make these methods static. You are not tracking any application state and can drop the overhead of instantiating your class.


Once you make those changes, just call:

User admin = MyUtility.createUser();
User someOtherUser = MyUtility.createUser('Standard User');

You should also annotate this class as @IsTest so it cannot be called outside a test context, and reduce the visibility to public.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .