7

I have a test class. The annotation on the class is @isTest(SeeAllData=false)

In the test, the first step I do is create a user. I am getting the following exception when I run my test.

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_COMM_NICKNAME, Duplicate Nickname.
Another user has already selected this nickname.
Please select another.: [CommunityNickname]

If SeeAllData is set to false, why would I get a duplicate nickname exception? Any ideas?

8

With V24.0 and higher, certain SObjects used in Org Setup are available to test methods even if SeeAllData=false. From the doc:

User
Profile
Organization
AsyncApexJob
CronTrigger
RecordType
ApexClass
ApexTrigger
ApexComponent
ApexPage

So, if your testmethod mocks a new user with a nickname already in use, it will be detected as a duplicate.

To avoid this, I tend to set mock User names, emails, aliases, nicknames, etc with leading or trailing special character like _ which is unlikely to be used in your org data

| improve this answer | |

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.