2

I noticed that when you create a user in a test class that the username must be unique in the real production environment. Most of my test classes are creating a user to then run the test method as.

The problem arises from the fact that you can create a test class, deploy it to production, and then have it break by someone else, coincidentally using that same username. Is there a best practice to avoid this besides trying to create long random usernames? Should we query for a random user in the org of that profile?

5
  • Why not use apextest@yourcompany.com or testuser@test.yourco.com Feb 21 '17 at 17:37
  • That would seem to increase the chance that an admin would create a test user with that name. Since we use communities there are a lot of test community users that have our company's domain.
    – brezotom
    Feb 21 '17 at 17:41
  • 1
    String username = string.valueOfGmt(System.now()); Feb 21 '17 at 17:44
  • 1
    I mock my user names starting with an underscore e.g. _mockuser@foo.com
    – cropredy
    Feb 21 '17 at 18:08
  • 1
    Remember that usernames just need to be in a form of a email address it doesn't need to be a resolvable email address. So that unleashes a host of possibilities. I would keep the domain as yours because as you may have found out someone created a user with test@test.com as a username. By using your own domain will ensure that others will not take it. For example I would never create a user with @brezo.com, but you might. Feb 21 '17 at 18:19
3

With following combination to generate unique Username, there were no errors while using runAs with deployments:

public static User createTestUser(Id roleId, Id profID, String fName, String lName) {
    String orgId = UserInfo.getOrganizationId();
    String dateString = String.valueof(Datetime.now())
        .replace(' ','').replace(':','').replace('-','');
    Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
    String uniqueName = orgId + dateString + randomInt;
    User tuser = new User(  firstname = fName,
                            lastName = lName,
                            email = uniqueName + '@test' + orgId + '.org',
                            Username = uniqueName + '@test' + orgId + '.org',
                            EmailEncodingKey = 'ISO-8859-1',
                            Alias = uniqueName.substring(18, 23),
                            TimeZoneSidKey = 'America/Los_Angeles',
                            LocaleSidKey = 'en_US',
                            LanguageLocaleKey = 'en_US',
                            ProfileId = profId,
                            UserRoleId = roleId);
    return tuser;
}

I had referred to some blog or community answer for this resolving issue way back. Don't have link of original post, but noticed that its now mentioned in Salesforce documentation here.

0

You can use System.now() as username with some prefix like

String username = 'Test'+string.valueOfGmt(System.now()); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.