+ Start a Discussion

TestdataUtility class creation

Hello all,
  Can any one help with creating utility Class in detail, with that i have to handle the record types also.
 I have basic idea about creating TestdataUtility class, i did not understand how to implement it.

 i have around five objects, recordtypes, and relationships  EX: A==B, A==>D,E  like that i have relaion ship ,

kindly suggest me the best way to create Testutility


Amit Chaudhary 8Amit Chaudhary 8
Please check below post how to create "Common Test Utility Classes for Test Data Creation"
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_utility_classes.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex_testing_1.htm
public class TestDataFactory {
    public static void createTestRecords(Integer numAccts, Integer numContactsPerAcct) {
        List<Account> accts = new List<Account>();
        for(Integer i=0;i<numAccts;i++) {
            Account a = new Account(Name='TestAccount' + i);
        insert accts;
        List<Contact> cons = new List<Contact>();
        for (Integer j=0;j<numAccts;j++) {
            Account acct = accts[j];            
            // For each account just inserted, add contacts
            for (Integer k=numContactsPerAcct*j;k<numContactsPerAcct*(j+1);k++) {
                cons.add(new Contact(firstname='Test'+k,
        // Insert all contacts for all accounts
        insert cons;
public class TestDataFactory {

    public static Invoice_Statement__c createOneInvoiceStatement(
                                                 Boolean withLineItem) { 
        // Create one invoice statement
        Invoice_Statement__c testInvoice = createInvoiceStatement();    
        if (withLineItem == true) {
            // Create a merchandise item
            Merchandise__c m = createMerchandiseItem('Orange juice');
            // Create one line item and associate it with the invoice statement.        
            AddLineItem(testInvoice, m);
        return testInvoice;
    // Helper methods
    private static Merchandise__c createMerchandiseItem(String merchName) {
        Merchandise__c m = new Merchandise__c(
            Description__c='Fresh juice',
        insert m;
        return m;
    private static Invoice_Statement__c createInvoiceStatement() {        
        Invoice_Statement__c inv = new Invoice_Statement__c(                
            Description__c='Test Invoice');      
        insert inv;
        return inv;
    private static Line_Item__c AddLineItem(Invoice_Statement__c inv, 
                                            Merchandise__c m) {
        Line_Item__c lineItem = new Line_Item__c(
                                    Invoice_Statement__c = inv.Id, 
                                    Merchandise__c = m.Id, 
                                    Unit_Price__c = m.Price__c, 
                                    Units_Sold__c = (Double)(10*Math.random()+1));
        insert lineItem;
        return lineItem;

NOTE:- in above class you can pass record type or create different method for each record type

let us know if that will help u