SmartFactory aims to provide test data objects with all required fields and object lookups pre-populated.
The initial version won the Mavens Consulting 2011 hackathon. As a 1-day project, the current state is more a proof of concept; please see Future Work if you'd like to contribute to developing into a fully functional solution.
For an easy, 1-click installation: SmartFactory for Force.com on Code Share.
To use the source code with a Salesforce org: How To Use Github and the Force.com IDE
To use with standard objects, no setup is required. 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);
To use with custom objects, you'll need to regenerate the SmartFactoryHelper class:
- In System Log, run "SmartFactory.generateHelperClass();"
- From the debug output, copy from "public with sharing class SmartFactoryHelper {" to "} // SmartFactoryHelper"
- Paste into the class SmartFactoryHelper in the Eclipse Force.com IDE or Setup > Develop > Apex Classes
SmartFactoryHelper now includes your custom objects, which can be used as follows:
Custom_Object__c customObject = (Custom_Object__c)SmartFactory.createSObject('Custom_Object__c');
See SmartFactory_Test for additional examples.
TODO comments note areas for additional development. Key areas include:
- Default field values for other Schema.DisplayTypes
- Create external service to dynamically generate SmartFactoryHelper class using Force.com Metadata API
- Create an app and setup tab to call the external service
- Provide an field override map that allows callers to specify default values for specific objects and fields
- Provide a recursion limit for lookups to the same object type
Also, please vote for Dynamic Class Instantiation and Casting on Ideaexchange, which would eliminate the need for a generated helper class.
For help and discussion, please use the project's Google Group.