1

while doing unit testing in Salesforce, how do you handle the test data?

Do you make new sets of data each time? (thats what Im doing right now)

Any other suggestions?

On a tangent, how do I quickly set up data for an object? Assuming Im not familiar with that object (like, what fields are required, if there's relationships, then which other objects I have to make data of?).

Thanks

1

Use test setup methods (methods that are annotated with @testSetup) to create test records once and then access them in any test method in the test class. Test setup methods are useful and can be time-saving when you need to create a common set of records that all test methods operate on or prerequisite data.

Check example how to use @testsetup annotation.

For quickly setting up data you have to identify the required fields for that object.If they are lookup or relationship fields then you have to create test data for those object as well.

Best approach is to create a TestUtil class, In which create methods which will setup data for given object.This way you will be able to reuse them.

An example format for this type of utility method could be following:

/**
     * Method: createUser
     * Description: Returns number of users created
     * @param: Integer  numUsers - specifies the number of users to be created
     *         String  ProfileType - Profile assigned to users
     *         String  sidkey - Locale sidKey for user 
     * @return: List<User> - List of users created
    **/
    public static List<User> createUser(Integer numUsers,String ProfileType,String sidkey) {

        Profile p = [SELECT Id FROM Profile WHERE Name like :'%'+ProfileType+'%' Limit 1];
        List<User> users = new List<User>();
        for (Integer k = 0; k < numUsers; k++) {
            users.add(new User(Alias = 'standt', Email='standarduser@testorg.com',EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey=sidkey, LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='standarduser'+Math.random()+'@testorg.com'));
        }
        return users;
    }
| improve this answer | |
  • Hey Mr.Frodo, thanks for fast reply! Is it possible to make all this data ONCE and have it available forever in any test class I ever make? Also, you have not used @testSetup or TestUtil in example? Thanks for reply, any info/knowledge is appreciated. – fraglord Jun 30 '16 at 7:27
  • @fraglord Actually testsetup data will be available for particular test class but not for other class.That's why i give advice to have TestUtil class.In which you will define methods to create test records once and then in each class you will just call those methods and insert the returned list. – Mr.Frodo Jun 30 '16 at 7:36
  • @fraglord i have added a link which has an example of testsetup method. – Mr.Frodo Jun 30 '16 at 7:40
  • Thank you! I will read the links you gave me. Hopefully its what I need. – fraglord Jun 30 '16 at 7:57

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.