Skip to content

Instantly share code, notes, and snippets.

@florianhoehn
Last active November 28, 2016 09:33
Show Gist options
  • Save florianhoehn/6a9eb31039a751da9d3f6a020951b0da to your computer and use it in GitHub Desktop.
Save florianhoehn/6a9eb31039a751da9d3f6a020951b0da to your computer and use it in GitHub Desktop.
So here is my interpretation of your SObjectBuilder or TestFactory :) - it ain't static btw !! I did not compile this...just jotted it down and put it together how it made sense to me. Probably makes sense to put a Util class back in for the describes and as you can see below using it should be really simple. I think it depends a bit if you like…
@isTest public with sharing class AccountBuilder {
private Account testAccount;
public AccountBuilder() {
this.testAccount = (Account)new SObjectBuilder('Account').build();
}
public AccountBuilder withRecordType(String recordTypeLabel) {
this.testAccount.RecordTypeId =
Schema.SObjectType.Account
.getRecordTypeInfosByName()
.get(recordTypeLabel)
.getRecordTypeId();
return this;
}
public AccountBuilder withName(String Name) {
this.testAccount.Name = Name;
return this;
}
public AccountBuilder insert() {
insert this.testAccount;
return this;
}
public Account build() {
return this.testAccount;
}
@IsTest
private static void AccountBuilder_insertAccount_Success() {
Test.startTest();
Account testAccount = new AccountBuilder()
.insert()
.build();
Test.stopTest();
System.assertNotEquals(null,
testAccount.Id);
}
}
@isTest public with sharing class ContactBuilder {
private Contact testContact;
public ContactBuilder() {
this.testContact = (Contact)new SObjectBuilder('Contact').build();
}
public ContactBuilder withRecordType(String recordTypeLabel) {
this.testContact.RecordTypeId =
Schema.SObjectType.Contact
.getRecordTypeInfosByName()
.get(recordTypeLabel)
.getRecordTypeId();
return this;
}
public ContactBuilder withFirstName(String firstName) {
this.testContact.FirstName = firstName;
return this;
}
public ContactBuilder withLastName(String lastName) {
this.testContact.LastName = lastName;
return this;
}
public ContactBuilder withEmail(String email) {
this.testContact.Email = email;
return this;
}
public ContactBuilder withContactType(String ContactType) {
this.testContact.Contact_Type__c = ContactType;
return this;
}
public ContactBuilder insert() {
insert this.testContact;
return this;
}
public Contact build() {
return this.testContact;
}
@IsTest
private static void ContactBuilder_insertContact_Success() {
Test.startTest();
Contact testContact = new ContactBuilder()
.insert()
.build();
Test.stopTest();
System.assertNotEquals(null,
testContact.Id);
}
}
// simple contact
Contact actualContact = new ContactBuilder()
.insert()
.build();
// simple contact related to simple account
Contact actualContact = new ContactBuilder()
.withAccount(new AccountBuilder()
.insert()
.build())
.insert()
.build();
// set some specific values you care about in code
Contact actualContact = new ContactBuilder()
.withFirstName('Florian')
.withLastName('Stealing Some Code')
.withAccount(new AccountBuilder()
.withName('Brain Trust')
.insert()
.build())
.insert()
.build();
@isTest public without sharing class SObjectBuilder {
private SObject testSObject;
public SObjectBuilder(String sObjectType) {
this.testSObject = Schema.getGlobalDescribe().get(objectType).newSObject();
this.populateRequiredFields(sObjectType);
}
public SObject build() {
return this.testSObject;
}
private void populateRequiredFields(String sObjectType) {
DescribeSObjectResult objectDescribe = Schema.getGlobalDescribe().get(sObjectType).getDescribe();
// Booleans can't be required. Lookups are too complex. Neither are considered here.
for (Schema.SObjectField soField : objectDescribe.fields.getMap().values()) {
Schema.DescribeFieldResult fieldDescribe = soField.getDescribe();
if (isRequired(fieldDescribe)) {
Schema.DisplayType displayType = fieldDescribe.getType();
if (isText(displayType)) {
defaultSObject.put(fieldDescribe.getName(), 'Default text');
}
else if (isNumber(displayType)) {
defaultSObject.put(fieldDescribe.getName(), 42);
}
else if (isDate(displayType)) {
defaultSObject.put(fieldDescribe.getName(), Date.today());
}
else if (isPhone(displayType)) {
defaultSObject.put(fieldDescribe.getName(), '5555555555');
}
else if (isEmail(displayType)) {
defaultSObject.put(fieldDescribe.getName(), 'defaultEmail@testdata.com');
}
else if (isUrl(displayType)) {
defaultSObject.put(fieldDescribe.getName(), 'https://mavens.com');
}
else if (isPicklist(displayType)) {
defaultSObject.put(fieldDescribe.getName(), fieldDescribe.getPicklistValues()[0].getValue());
}
}
}
}
private Boolean isRequired(Schema.DescribeFieldResult describeResult) {
return !describeResult.isNillable() && describeResult.isUpdateable();
}
private Boolean isText(Schema.DisplayType displayType) {
return displayType == Schema.DisplayType.String || displayType == Schema.DisplayType.TextArea;
}
private Boolean isNumber( Schema.DisplayType displayType) {
return displayType == Schema.DisplayType.Currency ||
displayType == Schema.DisplayType.Double ||
displayType == Schema.DisplayType.Integer ||
displayType == Schema.DisplayType.Percent;
}
private Boolean isDate(Schema.DisplayType displayType) {
return displayType == Schema.DisplayType.Date || displayType == Schema.DisplayType.DateTime;
}
private Boolean isPhone(Schema.DisplayType displayType) {
return displayType == Schema.DisplayType.Phone;
}
private Boolean isEmail(Schema.DisplayType displayType) {
return displayType == Schema.DisplayType.Email;
}
private Boolean isUrl(Schema.DisplayType displayType) {
return displayType == Schema.DisplayType.URL;
}
private Boolean isPicklist(Schema.DisplayType displayType) {
return displayType == Schema.DisplayType.Picklist || displayType == Schema.DisplayType.MultiPicklist;
}
}
@jpmonette
Copy link

jpmonette commented Nov 28, 2016

insert is a reserved word, so your class won't compile sadly if you use it as your method name :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment