When programming on the Salesforce platform in order to deploy anything to production you need to have a “Apex Test Class” that runs over 75% of the code without errors.
To make this task as painless as possible you should create whats called a Apex Utility class to use with test classes.
Let’s say you have a trigger that can be 100% covered with just an insert of an account record.
We can achieve that with this Utility Class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//Test Class @isTest public with sharing class TestUtility { public static Lead createLead() { Lead newLead = new Lead() ; newLead.FirstName = 'Salesforce'; newLead.LastName = 'Nick'; insert newLead; } public static Account createAcc() { Account newAccount = new Account() ; newAccount.Name = 'SalesforceNick'; insert newAccount; } } |
Now in your test class you could just do this (by invoking the utility class) and cover an account and lead trigger:
1 2 3 4 5 6 7 8 |
//Test Class @isTest private class Test_AccountTrigger { static testmethod void insertdata() { Lead leadObj = TestUtility.createLead(); Account accObj = TestUtility.createAccount(); } } |