In this Salesforce tutorial, we are going to learn about different sample test classes in Salesforce.

Testing is the key to successful long term development and is a critical component of the development process. And testing is key to the success of application particularly if application to be deployed to customers. If we validate that application works as expected, that there are no unexpected behaviours, then application will run for longtime.

There are two ways of testing an application.

  • One is through the salesforce user interfaces (testing with a single record)
  • Second way is to test for build functionality i.e testing with bulk records by using apex data loader up to 200 records can be passed through code.
     Before we can deploy code or package into production, the following must be true.

75 % of apex code must be covered by unit tests. All of those tests must complete successfully. When deploying to a production organisation we need to have 75% of apex code covered by tests.

Sample test classes in Salesforce

Test class in Salesforce example 1 :

@isTest
Public Class UpdateHandoffAttachedTest{
Static testMethod void HandoffAttached(){ 
Account a = new Account();
a.Name='Test Account';
 a.phone='8686864286';
 a.Rating='Hot';
insert a;
//Create a new Opportunity 
Opportunity opp=new Opportunity();
opp.Name='Test Oppty'; 
Opp.StageName='Prospecting';
Date myDate = Date.newinstance(‘2012, 2, 17’); 
opp.CloseDate=myDate;
opp.Accountid=a.id;
insert opp;
//Create Top X Designation with Positive scenario 
Top_X_Designation__c t = new Top_X_Designation__c();
t.Type__c='Contract Flow Down/Handoff';
 t.Document_Attached__c=True; t.Opportunity__c=opp.id;
insert t;
Opportunity o1 = [Select Handoff_Attached__c from Opportunity where id=:opp.id ]; 
System.assertEquals('Yes',o1.Handoff_Attached__c);
//Update Top X Designation with Negative scenario 
t.Type__c='Contract Flow Down/Handoff'; t.Document_Attached__c=False; t.Opportunity__c=opp.id;
Update t;
Opportunity o2 = [Select Handoff_Attached__c from Opportunity where id=:opp.id ]; 
System.assertEquals('No',o2.Handoff_Attached__c);
delete t;
Opportunity o3 = [Select Handoff_Attached__c from Opportunity where id=:opp.id ];
 System.assertEquals(null,o3.Handoff_Attached__c);
}

Test class in Salesforce example 2

@isTest
Public Class PrefixDoctorTest{
Public static testMethod void PreDoctor(){ 
Lead l=new Lead();
l.LastName='Anil';
l.company='Wipro';
l.Status='Open - Not Contacted'; 
insert l;//inserting a single record 
}
}

Test class in Salesforce example 3

@isTest
Public Class ContactsCreationTest{
 static testMethod void CreateContacts(){
 Account a = new Account();
a.name='Test Account';
 a.phone='9**********'; 
a.Rating='Hot'; 
a.Number_of_Locations__c=4;
insert a;
List<Contact> lc=[Select id from Contact where AccountId=:a.id];
 System.assertEquals(a.Number_of_Locations__c, lc.size());
}
 }

Test class in Salesforce example 4

@isTest
Public Class CreateCRonContactCreationTest{ 
static testMethod void CreateContact(){
//Inserting Contacts with Postive Scenario
 Contact c1 = new contact();
c1.LastName='Anil Reddy'; 
c1.Contact_Relationship__c=True; insert c1;
List<Contact_Relationship__c> ConRel1 = [SELECT Id FROM Contact_Relationship__ c WHERE Contact__r.Id =:c1.id];
//verify that Contact Relationship was created for above contact System.assertEquals(1,ConRel1.size());
// Inserting Contacts with Negative Scenario 
Contact c2=new Contact();
c2.lastName='Sateesh Reddy'; 
c2.Contact_Relationship__c=false;
 insert c2;
List<Contact_Relationship__c> ConRel2 = [SELECT Id FROM Contact_Relationship__ c WHERE Contact__r.Id =:c2.id];
System.assertEquals(0,ConRel2.size());