Technology moves fast! ⚡ Don't get left behind.🚶 Subscribe to our mailing list to keep up with latest and greatest in open source projects! 🏆


Subscribe to our mailing list

SmartFactory-for-Force.com

Salesforce test data generator fills lookups, master-detail relationships, and required fields. Native Force.com Apex code using dynamic metadata.

Subscribe to updates I use SmartFactory-for-Force.com


Statistics on SmartFactory-for-Force.com

Number of watchers on Github 196
Number of open issues 4
Average time to close an issue 12 months
Main language Apex
Average time to merge a PR 2 days
Open pull requests 1+
Closed pull requests 2+
Last commit about 2 years ago
Repo Created about 8 years ago
Repo Last Updated over 1 year ago
Size 48 KB
Organization / Authormbotos
Contributors7
Page Updated
Do you use SmartFactory-for-Force.com? Leave a review!
View open issues (4)
View SmartFactory-for-Force.com activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating SmartFactory-for-Force.com for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

Salesforce Test Data Generator

Salesforce test data generator fills lookups, master-detail relationships, and required fields.

Native Force.com Apex code using dynamic metadata.

Quickly Generate Lookups, Master-Detail Relationships, and Required Fields

How often have you wasted hours reverse-engineering a new schema just to create data for a unit test?

Task__c testTask = new Task__c();
ERROR: Field Desciption__c required on Task__c

Task__c testTask = new Task__c(Description__c = 'Create unit test data');`
ERROR: Lookup relationship Project__c required on Task__c`

Project__c testProject = new Project__c();
Task__c testTask = new Task__c(Project__c = testProject, Description__c = 'Create unit test data');
ERROR: Field Status__c required on Project__c
...

Watch SmartFactory dynamically use the Describe metadata to populate all required fields with valid data and create any related objects:

Task__c testTask = (Task__c)SmartFactory.createSObject('Task__c');

Learn Salesforce Testing Best Practices

3 Principles of Salesforce Testing includes production code by Salesforce MVP Matthew Botos.

The Evolution of Test Data provides more background on the problem and SmartFactory's solution.

SmartFactory's first version won the Mavens Consulting 2011 Hackathon in less than a single day of coding.

Easy 1-Line Usage

Just use SmartFactory in your tests to create objects:

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

To cascade and create objects for lookup and master-detail relationships:

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.

Powerful Options

Include or Exclude Fields

To fill only required and included fields:

SmartFactory.FillAllFields = false;
SmartFactory.IncludedFields.put('Account', 'AccountNumber');

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

To fill all fields but those excluded:

SmartFactory.FillAllFields = true;
SmartFactory.ExcludedFields.put('Account', 'AccountNumber');

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

Validation Rules

To set specific values to pass your custom Validation Rules, wrap SmartFactory with your own class like this:

public class TestObjectFactory {
  public static Account createAccount() {
    Account account = (Account)SmartFactory.createSObject('Account');
    account.Customer_Terms__c = 'Net 30';
    return account;
  }
}

You can then call that reusable method from your tests. If you have validation rules on Account or Contact, you may also need to modify SmartFactory_Test.

Logging

To prevent the large number of system calls from filling your debug log, you can set logging filter overrides for the SmartFactory class: Setup - Develop - Apex Classes - SmartFactory - Log Filters - System = NONE.

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.

SmartFactory-for-Force.com open issues Ask a question     (View All Issues)
  • almost 8 years Have transparent support for caching non cascading objects
  • almost 8 years Fix default value used for PickList fields
  • almost 8 years Add support for default values during object creation
SmartFactory-for-Force.com open pull requests (View All Pulls)
  • Fixes for Issues 5,6,7 & 8
SmartFactory-for-Force.com list of languages used
Other projects in Apex