Sunday, October 28, 2012

Salesforce: Unified object creator.

Everytime when we write test classes, we need to a create a big number of data to apply them to the methods that we are testing. We create a lot of methods leading to the creation of these or those objects. I have created the method leading to the creation of any type objects we need:
public with sharing class Utilities {
    /*
        Method for creating any Object
    */
    
    public static List<sObject> createObject(Integer numberToCreate, 
                                             Boolean doInsert, 
                                             Schema.SObjectType objectType, 
                                             Map<String, String> fieldsMap){    
        List<sObject> objectsList = new List<sObject>();
        List<String> objectFields = new List<String>();
        List<String> fieldsList = new List<String>();
        
        objectFields.addAll(objectType.getDescribe().fields.getMap().keySet());
        fieldsList.addAll(fieldsMap.keySet());
        
        if(numberToCreate > 0){
            for( Integer countOfObjects = 0; countOfObjects < numberToCreate; countOfObjects++ ){
                SObject currentObject = objectType.newSObject();
                
                for(String fieldFromList : fieldsList){
                    for(String fieldFromObject : objectFields){
                        if(fieldFromObject == fieldFromList){
                            currentObject.put(fieldFromList , fieldsMap.get(fieldFromList));
                        }
                    }
                }    

                objectsList.add(currentObject);
            }

            if ( doInsert ) insert objectsList;
        }
        
        return objectsList;
    }
}
Also I have added test class for it:
@isTest
private class TestUtilities {
    private static final Integer TEST_RECORD_COUNT = 10;
    
    static Map<String, String> fieldsMap;    
    static Schema.SObjectType objectType;
    static List<String> objectFields = new List<String>();
    static String query;
        
    private static void setup(){
        objectType = Account.getsObjectType();
        objectFields.addAll(objectType.getDescribe().fields.getMap().keySet());
        fieldsMap = new Map<String, String>{'Name' => 'Test',            //Fill in fields
                                            'BillingCountry' => 'Usa',    //IMPORTANT You need to fill in requaired fields
                                            'AccountSource' => 'Web'};
    }
    
    private static testmethod void testCreateObject(){
        List<sObject> testCreatedObjects;
        List<sObject> objectsFromDB;
        List<String> fieldsFromBD = new List<String>();
        
        setup();
        
        Test.startTest();
            testCreatedObjects = Utilities.createObject(TEST_RECORD_COUNT,
                                                           true,
                                                           objectType,
                                                           fieldsMap);
        Test.stopTest();
        
        query = 'SELECT Id';
        
        fieldsFromBD.addAll(fieldsMap.keySet());
        
        for(String fieldDB : fieldsFromBD){
            for(String fieldObject : objectFields){
                if(fieldDB == fieldObject){
                    query += ', ' + fieldDB;
                }
            }
        }
        
        query += ' FROM ' + objectType + ' WHERE Id IN : ' + 'testCreatedObjects' +  ' ';
        
        objectsFromDB = Database.query(query);        
        
        System.assertEquals( TEST_RECORD_COUNT , objectsFromDB.size() );
    }
}
You can also add compare of every field it test.

Enjoy.

No comments:

Post a Comment