Skip to content

Instantly share code, notes, and snippets.

@cdcarter
Last active May 29, 2016 04:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdcarter/6d5a9a99979ae8ef90f9a777b04bc308 to your computer and use it in GitHub Desktop.
Save cdcarter/6d5a9a99979ae8ef90f9a777b04bc308 to your computer and use it in GitHub Desktop.
QuickData

QuickData

QuickData is a (very hacky) plumbing class that loads realistic test data from any SObject. Mockaroo provides a very pleasant and powerful toolkit for creating test data. QuickData generates data from a saved Mockaroo schema and inserts it as is into Salesforce.

Setting Up Mockaroo

You'll require a free account with Mockaroo to be able to save schemas and use the API. Once you set up a free account, start building fields for your SObject. You can see my sample schema for Lead at https://www.mockaroo.com/20689c00. The output format must be configured as a JSON Array, as in my example. Your Mockaroo field names must exactly match your Salesforce field names. If you'd like to hide a field in the schema from your output to salesforce, put two __s in front of it's name, and it will not be mapped.

Use it

QuickData.createRecords('MOCKAROOAPIKEY','20689c00',20,List<Lead>.class);

The fourth parameter of the call is the Type object for a list of your SObject (e.g. List<Opportunity>.class or List<Shark__c>.class)

public class QuickData {
/* QuickData inserts your Mockaroo Schema, no questions asked. Remote Site needed.
* USAGE: QuickData.createRecords('MOCKAROOAPIKEY','20689c00',20,List<Lead>.class);
* Creates 20 leads using the schema at https://www.mockaroo.com/20689c00 */
public static List<SObject> createRecords(String a, String s, Integer c, Type t) {
String fmtStr = 'https://www.mockaroo.com/{1}/download?count={2}&key={0}';
List<String> reqDetails = new List<String>{a,s,String.valueOf(c)};
string requestString = String.format(fmtStr,reqDetails);
HttpRequest req = new HttpRequest();
req.setEndpoint(requestString);
req.setMethod('GET');
HttpResponse resp = (new Http()).send(req);
if(resp.getStatusCode() == 200) {
List<SObject> objs = (List<SObject>)JSON.deserialize(resp.getBody(), t);
upsert objs;
return objs;
}
return null;
}
}
@isTest global class QuickData_TEST {
global class QuickDataCalloutMock implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest request) {
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
response.setBody('[{"FirstName": "Anne","LastName": "Stone","Email": "astone0@about.me","Company": "Feednation","Title": "Human Resources Manager"}, {"FirstName": "Anne","LastName": "Porter","Email": "aporter1@marketwatch.com","Company": "Kayveo","Title": "Pharmacist"}, {"FirstName": "Laura","LastName": "Cooper","Email": "lcooper2@xing.com","Company": "Yacero","Title": "Pharmacist"}, {"FirstName": "Ryan","LastName": "Owens","Email": "rowens3@paypal.com","Company": "Wordify","Title": "Speech Pathologist"}, {"FirstName": "Jane","LastName": "Collins","Email": "jcollins4@xrea.com","Company": "Yacero","Title": "Research Associate"}]');
response.setStatusCode(200);
return response;
}
}
@isTest global static void test_calloutLeadCreation(){
Test.setMock(HttpCalloutMock.class, new QuickDataCalloutMock());
Test.startTest();
QuickData.createRecords('apikey', 'schema', 5, List<Lead>.class);
Test.stopTest();
System.assertEquals(5,[SELECT Id FROM Lead].size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment