Creating unit tests without interacting with the database - creating fake IDs
Here's the fake ID test utility method as used in my DF13 "Tests and Testability" session:
public class TestUtility
{
static Integer s_num = 1;
public static String getFakeId(Schema.SObjectType sot)
{
String result = String.valueOf(s_num++);
return sot.getDescribe().getKeyPrefix() +
'0'.repeat(12-result.length()) + result;
}
}
Originally posted here: http://foobarforce.com/2013/08/15/apex-method-of-the-day-string-repeat/
And here is a use of it:
Account a = new Account(Id=TestUtility.getFakeId(Account.SObjectType));
Opportunity o = new Opportunity(AccountId=a.Id);