How can i create an user with system administrator profile when seeAlldata = false in a test class

Prady picture Prady · Jul 25, 2012 · Viewed 8.5k times · Source

I want to create an User in test class with system Administrator profile.

The issue which i have is that i have seeAllData = false where in the test class would only use data from within the test class.

 List<Profile> ps = [select id, name from Profile where  name = 'System Administrator'];

This might not return any values.

Any ideas how we can create an user

EDIT

I can get the data for the query on even with seeAllData= false. So is it that Profile records are visible even if SeeAllData = false ?

Answer

JCD picture JCD · Jul 25, 2012

According to the documentation, the User and Profile objects are considered "objects that are used to manage your organization" and can be accessed regardless of whether or not your test class/method includes the IsTest(SeeAllData=true) annotation.

Other objects that are accessible in this manner include:

  • Organization
  • RecordType
  • ApexClass
  • ApexTrigger
  • ApexComponent
  • ApexPage