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

            This is the interface class

            Provides the json strings that will be serialised into sobjects

            Serialises json strings from TestDataJsonLibrary or custom json strings

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

            Inserts and then updates a record

            Inserts multiple records of different Sobjects and links the records together

            Inserts multiple records

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

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


            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 )

            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.