kalyanlanka / SmartFactory-for-Force.com

Creates test data objects with all required fields and object lookups pre-populated.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SmartFactory for Force.com

SmartFactory aims to provide test data objects with all required fields and object lookups pre-populated.

The idea came from writing unit tests for unfamiliar orgs, where setting up test data might mean digging through a large hierarchy of lookup relationships and required fields. Instead, SmartFactory uses the Describe metadata to populate all fields with data of the right type. For lookup fields, it creates an appropriate object and then uses that object's id.

The Evolution of Test Data provides an introduction to the problem and SmartFactory's solution.

The initial version won the Mavens Consulting 2011 hackathon.

Installation

For an easy, 1-click installation: SmartFactory Unmanaged Package (sandbox)

To use the source code with a Salesforce org: GitHub Salesforce Deploy Tool

To prevent the large number of system calls from filling the debug log, you may also want to set logging filter overrides for the SmartFactory class. (Setup - Develop - Apex Classes - SmartFactory - Log Filters - System = NONE)

Usage

Just use SmartFactory in your tests to create objects:

Account account = (Account)SmartFactory.createSObject('Account');

To cascade and create lookup objects:

Contact contact = (Contact)SmartFactory.createSObject('Contact', true);

The same syntax is used for custom objects:

Custom_Object__c customObject = (Custom_Object__c)SmartFactory.createSObject('Custom_Object__c');

See SmartFactory_Test for additional examples.

Future Work

TODO comments note areas for additional development. Key areas include:

  1. Provide an field override map that allows callers to specify default values for specific objects and fields
  2. Provide a recursion limit for lookups to the same object type

Help and Discussion

For help and discussion, please use the project's Google Group.

About

Creates test data objects with all required fields and object lookups pre-populated.

License:Apache License 2.0


Languages

Language:Apex 100.0%