Search

Sunday, 24 May 2015

Unit Test Data Creation Framework


Ive worked in so many companies where developers copy and paste code from 1 unit test to another to create test data for the testmethod. Then when they need to add a new field because it is a new required field that is failing tests they realise they've got to copy to all classes.

Some companies improve by having a central class but the functions they make have passed in arguments for each field, so for each field that needs to be passed in requires a change to this class.

Some improve this by passing in a list of a key value pair classes. This will become clearer later when we start coding in my follow on blog to this one.

Some may improve even further by having a for loop to create as many records as you like.

But Ive never seen a company to have all the benefits above plus using json to serialise and de-serialise to / from generic sobjects. Use OOP to provide a logical framework separating functionality that provide only returning Casted Sobjects, inserting single objects, inserting multiple objects for bulk testing, updating objects, inserting multiple objects of different types and linking them together, and inserting multiple objects creating complex linked data structures.

Building into the framework a highly flexible capability so whatever type of test data is required the framework can easily create.

Also to provide an option for rapid transactional processing to speed up unit tests and also speed up deployments.

The Framework can also be used to create data in Salesforce for many purposes such as data migration, exposing for web services etc.

So lets get started.
First create a number of classes

ITestData
            This is the interface class

TestDataJsonLibrary
            Provides the json strings that will be serialised into sobjects

TestDataReturnData
            Serialises json strings from TestDataJsonLibrary or custom json strings

TestDataInsertData
            DML transactions of the serialised json strings occurs here, but only inserts 1 record

TestDataUpdateData
            Inserts and then updates a record

TestDataComplexData
            Inserts multiple records of different Sobjects and links the records together

TestDataBulkData
            Inserts multiple records

UtilDML
            A utility class used to assign values to any fields of any Sobject

KeyValue
            This contains information of fields, their api name, value and data type

KeyValueBulk

            This contains information in KeyValue, how many records to create for each  object and whether or not to bypass code in triggers for rapid transactional  processing ( the default is not to bypass triggers )

TriggerController
            If you have any code in triggers also create this class

In my next blog I will start showing you the coding

No comments:

Post a Comment

Note: only a member of this blog may post a comment.