Home / Salesforce

Test Class With Example In Salesforce

Rating: 5.0Blog-star
Views: 15635
by Arogyalokesh
Last modified: March 27th 2021

Test Class Explanation:

Apex testing framework ensures that we can write and execute tests for all of our Apex Classes and triggers in the Force.com platform. Apex unit testing makes sure that your Apex code is with high quality and meets the requirements in deploying Apex.

In the testing framework, your code is tested and the testing code is coded in the sandbox environment, and then deployed to production Org. Apex unit tests are also required for distributing and deploying the Apex in Salesforce Org. The Apex code ensures that it works as expected. We write the unit testing code in order to find the bugs in our code and for fixing the output. Testing enables the code coverage, and in order to this, first, we need to know what is Code Coverage. Let’s learn what code coverage is now. Code Coverage is nothing but the percentage of code working. The minimum code to be covered is 75% in order to deploy in production from Sandbox.

Want to enhance your skills in dealing with the world's best CRM, enroll in our Salesforce Online Course

The below points define the best practice for test class:

  • All test methods should be in a separate class in which the method being resided.
  • @isTest should be used to define the test class.
  • The system.assertEquals defines what is the expected output.
  • The test data should be done from a utility class.
  • To run the code to a specific user annotation @Test.runAs.
  • Testing should include Test.startTest and Test.stopTest for an asynchronous method of testing. 

Program# Test Class Sample For Explanation: For a detailed explanation of the “Test Class”, we will consider a sample program. The detailed explanation includes three programs, Main Class, Trigger Class, and APEX Class for execution.

APEX Trigger Program:

APEX Trigger Program

Trigger Helper Program:

Test Class Program:


[Related Article: Trigger Scenarios for practice in Salesforce]

Explanation of the program line by line:

@isTest - It is defined as Annotation. With this declaration, the “Run Test” button will be enabled in the program after “Save”. By this, the Salesforce will understand that this is a Test Class.

The coding limit is calculated in “Characters”. After making use of all the storage level, we need to buy storage space from Salesforce, or else we will be getting Storage Error.

For every APEX class, we need to write Test Class also. Test Class will not be considered for storage purpose by Salesforce as we have included @isTest in the program. This is a forceful work we do not do for any business purpose.

How to check for limit of the Salesforce Environment for characters to save?

Login to Salesforce Org → Setup → Build → Develop → APEX Class → Percent of Apex Used : 0.1%.

The Below image is a sample figure which describes the Total Storage:                

Public Class JeanClassDemonstartionTest{} - It is just the declaration of the class.

Static testMethod void MyTestFunction(){} - “testMethod” is the keyword used to declare for all Test Class for future use as per the rule of Salesforce.

//create new record data
Levis__c j = new Levis__c();
//same like, list MyList=New list();
j.Name = 'John';
j.Price__c = 1000;

The above program lines insert new record by name John and his purchase price of 1000rs/- by programming.

//Insert Jean
Insert j;

Frequently Asked Salesforce Interview Questions & Answers

To save any record by code, the code is shown above, and it is called as DML(Data Manipulation Language) used to save data in Salesforce.Org

//how to retrieve the new Jean
Levis__c j2 = new Levis__c();
j2 = [SELECT Price__c FROM Levis__c WHERE id =: j.Id];

For fetching the data which is inserted in the DataBase.

Note: In programming, we have a few important sections to observe for Test Class Execution i.e @isTest, Function Name, Class Name.

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

In the next topic, we will discuss in detail about “Code Coverage For PROD In Salesforce”. Keep following us for more info on Salesforce Development / Programming.

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Salesforce Administration Training  Salesforce Lightning Training 
Salesforce Advanced Developer Training  Salesforce Developer Training 
Salesforce IoT Training  Salesforce App Builder Certification Training 
Salesforce AppExchange Training  Salesforce Service Cloud Training 
and many more.   

About Author

Author Bio

Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.