4

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.

@AuraEnabled
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
    users.add(u);

    //Get all users in org and add to list
    List<User> addedUsers = [SELECT Id, Name FROM User];
    users.addAll(addedUsers);

    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.

@isTest
class WhiteboardItemsApexControllerTest {
static testMethod void whiteboardItemDBTest() {

    User u      = new User();
    u.UserName  = 'FirstName.LastName@company.com';
    u.Email     = 'FirstName.LastName@company.com';
    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:{Username=FirstName.LastName@company.com, Email=FirstName.LastName@company.com, 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?

13

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 '16 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 '16 at 8:59

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.