A method in my class I'm testing is suppose to return all users in the org, plus an extra user empty user at the beginning of the list. The reason for this is I wanted the default to be empty.

public static List<User> getUsersDB() {
    // Create empty user
    User u = new User();
    // Create empty list for users
    List<User> users = new List<User>();
    //Add empty user to list
    //Get all users in org and add to list
    List<User> addedUsers = [SELECT Id, Name FROM User];
    return users;

This is the results on the front end enter image description here

Now I'm trying to test this method in my test class.

class WhiteboardItemsApexControllerTest {
static testMethod void whiteboardItemDBTest() {
    User u      = new User();
    u.UserName  = '[email protected]';
    u.Email     = '[email protected]';
    u.FirstName = 'FirstName';
    u.LastName  = 'LastName';
    u.Alias     = 'flast';
    u.ProfileId = '00ed0000000uSiX';
    u.TimeZoneSidKey = 'GMT';
    u.LocaleSidKey = 'en_US';
    u.EmailEncodingKey = 'ISO-8859-1';
    u.LanguageLocaleKey = 'en_US';
    u.UserPermissionsMobileUser = false;
    insert u;
    User u2 = new User();
    List<User> users = new List<User> {u2, u};
    System.assertEquals(users, WhiteboardITemsApexController.getUsersDB());



For some strange reason, it's returning all the users in my org, in addition to the test users I created.

System.AssertException: Assertion Failed: Expected: (User:{}, User:{[email protected], [email protected], FirstName=FirstName, LastName=LastName, Alias=flast, ProfileId=00ed0000000uSiXAAU, TimeZoneSidKey=GMT, LocaleSidKey=en_US, EmailEncodingKey=ISO-8859-1, LanguageLocaleKey=en_US, UserPermissionsMobileUser=false, Id=005d0000006FY7QAAW}),

Actual: (User:{}, User:{Id=005d0000002kt1DAAQ, Name=Leigh Blair}, User:{Id=005d0000000xloEAAQ, Name=Zack Woodmansee}, User:{Id=005d0000006FY7QAAW, Name=FirstName LastName}, User:{Id=005d0000005NR4WAAW, Name=Tyler Zika}, User:{Id=005d0000000xlRkAAI, Name=Chatter Expert})

Why is that?

1 Answer 1


Isolation of Test Data from Organization Data in Unit Tests

Starting with Apex code saved using Salesforce API version 24.0 and later, test methods don’t have access by default to pre-existing data in the organization, such as standard objects, custom objects, and custom settings data, and can only access data that they create. However, objects that are used to manage your organization or metadata objects can still be accessed in your tests such as:

  • User
  • Profile
  • Organization
  • AsyncApexJob
  • CronTrigger
  • RecordType
  • ApexClass
  • ApexTrigger
  • ApexComponent
  • ApexPage
  • 2
    and, useful to know, StaticResource.
    – cropredy
    Aug 19, 2016 at 6:15
  • Right. Seems obvious given how Test.loadData works. Didn't even notice it wasn't on the list.
    – Adrian Larson
    Aug 19, 2016 at 8:59

You must log in to answer this question.

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