Friday 13 October 2017

Salesforce Test Class Template

Salesforce Test Class Template :



/*********************************************************************************
Class Name: TestClassTemplate

Purpose:   This is the test class for testing related apex class.

Please maintain only the last 5 history changes/modifications in the audit log

History of Changes:           
--------------------------------------------------------------------------------------------------------------------------
Date                                          Developer                               Comments
--------------------------------------------------------------------------------------------------------------------------
07-Feb-2017                                 Developer Name                         Added new method
********************************************************************************/
@isTest
public class TestClassTemplate {

    /*
* MethodName     : testData
* Description    : Create Testdata in @testSetup, use same data in all testmethods of this class.
*/
    @testsetup
    public static void testData(){
        list<account> lstAccs = new list<account>();
        list<contact> lstCons = new list<contact>();
   
        //Sample test data inserting here:
        for(integer i =0; i<10;i++){
            Account accData = new Account(
                name = 'testAcc'+i,
                Phone = '33333344444',                    // Phone
                Industry = 'Energy',                        // Industry
                Type = 'Prospect',                          // Type
                Match_Billing_Address__c = false,           // Match Billing Address
                Number_of_Contacts__c = 1,                  // Number of Contacts
                Description = 'Smith Enterprises :: null'  // Description
           
            );
            lstAccs.add(accData);
       
        }
        insert lstAccs;
   
        integer i = 0;
        for(account acc : lstAccs){
            Contact con = new Contact(
                lastname = 'testingcons'+i,
                Email = 'varam@gmail.com',               // Email
                Accountid = acc.id,                      // Account Name
                AssistantPhone = '1231234561',           // Asst. Phone
                Title = 'CEO',                           // Title
                Birthdate = system.today(),              // Birthdate       
                MobilePhone = '9874561235'              // Mobile
            );
            i++;       
            lstCons.add(con);
        }
        insert lstCons;
   
    }

    /*
* MethodName     : methodTest
* Description    : testing class methods.
*/

    public static testmethod void methodTest(){
        //Querey TestData
        list<contact> ltConsdata = [select id,firstname,lastname,accountid,account.name from contact];
   
        //Use assert statements
        system.assertEquals(10, ltConsdata.size())
       
            //Use test.startTest() and stopTest() for refreshing governor limits.
            test.startTest();
        classname.method(ltConsdata);
        test.stopTest();
   
    }

}






Complete Salesforce CPQ training Free videos

Salesforcestart:: We are excited to announce that our YouTube channel, Salesforcestart, is your one-stop-shop for all things Salesforce CPQ!...