Maximize Installs & Minimize Apex Test Failures | Salesforce Developers Blog

It’s a scenario many of us have been through.  You’ve slaved over your application for months, written impeccable tests, and verified everything works, but customers still can’t install the application because of test failures resulting from their validation rules and triggers preventing your test data from being created.  Even worse, you get a 1 star rating as they vent their frustrations.

For Chatter Unfollow Rules v2.0, I came up with a different approach to avoid this issue.  The app uses dynamic SOQL to let you pick which object, field, and criteria, similar to workflow rules, and then unfollows the records that match the criteria.  Since it works for any object and custom object, I realized I no longer had to be shackled to standard objects for test data and created a custom object for all of my tests, with custom fields for each of the field types supported by my app.  Even better, for Aloha apps, the objects don’t count towards app limits, so there’s no downside to the additional object.

I then had an easy way to make test data that would verify the app works as expected, and customers were able to install without issues.  An added side benefit is that patch upgrades now succeed for everyone as well, so I can push bug fixes without agonizing over whether to ignore apex test failures, because there aren’t any!

If you want to see the approach in more detail, check out the app here and open the UnfollowTests class:
Chatter Unfollow Rules – Unmanaged

Utility method for creating dummy test data:

public static List<UnfollowTest__c> createUnfollowTestRecords(Long numRecords,Boolean check, Date dat, DateTime datTim, Decimal dec, String em, String ph, String pick, string str,String ur ) {
List<UnfollowTest__c> testRecords = new List<UnfollowTest__c>{};
Integer i;
for (i=0;i<numRecords;i++){
UnfollowTest__c t = new UnfollowTest__c(Name='test'+i, String__c=str,Checkbox__c = Check,Date__c= dat, DateTime__c=datTim,  Decimal__c=Dec, Email__c=em, Phone__c=ph, Picklist__c=pick, URL__c=ur);
insert testRecords;
return testRecords;

Stay up to date with the latest news from the Salesforce Developers Blog