Home  >  Blog  >   Salesforce  > 

Test Class in Salesforce With Example

Rating: 5
  
 
15956
  1. Share:
Salesforce Articles

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 of 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 do 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 Online Salesforce Course Certification

The below points define the best practice for test class:

  • All test methods should be in a separate class in which the method is 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.

MindMajix's Youtube Channel

APEX Trigger Program:

APEX Trigger Program

Trigger Helper Program:

Test Class 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, Salesforce will understand that this is a Test Class.

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

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

How to check for a 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 a new record by the name of John and his purchase price of 1000rs/- by programming.

//Insert Jean
Insert j;

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.

In the next topic, we will discuss in detail “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 
Salesforce Marketing Cloud Training Salesforce Vlocity Training
and many more.   
Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
Salesforce TrainingJan 29 to Feb 13
Salesforce TrainingJan 31 to Feb 15
Salesforce TrainingFeb 05 to Feb 20
Salesforce TrainingFeb 07 to Feb 22
Last updated: 20 Jan 2022
About Author
Remy Sharp
Arogyalokesh

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.

Recommended Courses

1 / 15