0

Created a DataFactory class to reuse the code in test classes. I am not able to get the List of users record id into the test class. Can anyone please help me with this?

It is currently throwing Error : Initial term of field expression must be a concrete SObject: List

TestDataFactory Apex class:

@isTest
public class testDataFactory{
    public Static List<User> cuser(){

        List<User> UsersList = new List<User>();

        Profile p = [SELECT Id FROM Profile WHERE Name='Profile1'];
        User usr1 = new User(Alias = 'aa', Email='[email protected]',
        EmailEncodingKey='UTF-8', FirstName='yhsl',LastName='test', LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]');
        UsersList.add(usr1);
        User usr2 = new User(Alias = 'ab', Email='[email protected]',
        EmailEncodingKey='UTF-8', FirstName='iuqwet',LastName='Test', LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]');
        UsersList.add(usr2);
        insert UsersList;
        return UsersList;
    }

}

Test Class:

@isTest
private class TestClass
{
    Account Acc = new Account(Name='Test');
    Insert Acc;
    Acc.ownerId = testDataFactory.cuser().UsersList[0].id; // It is throwing Error : Initial term of field expression must be a concrete SObject: List<User>
    update Acc; 
} 
2
  • Once insert the user, query User object again and populate List<User> UsersList
    – SL man
    Jan 10, 2017 at 20:32
  • check my answer if that approach helps Jan 10, 2017 at 20:45

3 Answers 3

2

The class TestClass does not have access to the UsersList variable in the current configuration, since UsersList only has scope in the cuser() method (that is, it doesn't exist outside of the method). Rather than trying to access UserList directly, try accessing the reference to the list that is returned by the method. Change

Acc.ownerId = testDataFactory.cuser().UsersList[0].id;

to

Acc.ownerId = testDataFactory.cuser()[0].id;
1

The TestDataFactory.cuser method returns a List<User>. This type has no property named UsersList. To get the first element, simply index directly into the returned list.

User someUser = testDataFactory.cuser()[0];
Id someUserId = testDataFactory.cuser()[0].Id;
0

You can take this approach to build the test classes.

public class TestDataFactory{

    public Static void cuser()
    {
        List<User> UsersList = new List<User>();

        Profile p = [SELECT Id FROM Profile WHERE Name='Profile1'];
        User usr1 = new User(Alias = 'aa', Email='[email protected]',
        EmailEncodingKey='UTF-8', FirstName='yhsl',LastName='test', LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]');
        UsersList.add(usr1);
        User usr2 = new User(Alias = 'ab', Email='[email protected]',
        EmailEncodingKey='UTF-8', FirstName='iuqwet',LastName='Test', LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]');
        UsersList.add(usr2);
        insert UsersList;       
    }
}

You test class will have @testSetup method which will create users.

And you can use those created user records later in any SOQL query or to run test Method using System.runAs()

@isTest
public class TestDataFactory_Test
{
    /*
    * This method prepares the test data of this class.
    */
    @testSetup static void prepareSetupData() 
    {
        //create user records
        TestDataFactory.cuser();           
    }

    public static testMethod void runTest()
    {
        User userObj = [SELECT Id FROM User WHERE Email = '[email protected]'];
        System.runAs(userObj)
        {
            Account Acc = new Account(Name='TestVAccount12');
            Insert Acc;
            System.assert(...);

            Acc.ownerId = userObj.id; 
            update Acc; 
            System.assert(....);

        }

    }    
}

You must log in to answer this question.

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