Hurry! 20% Off Ends SoonRegister Now

Test Class in Salesforce With Example

Rating: 4.8
16970

Test Class Explanation

The 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 the 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 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:

Blog post image

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 an 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:                

Blog post image

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 TrainingSalesforce Vlocity Training
and many more.  

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
Salesforce TrainingDec 21 to Jan 05View Details
Salesforce TrainingDec 24 to Jan 08View Details
Salesforce TrainingDec 28 to Jan 12View Details
Salesforce TrainingDec 31 to Jan 15View Details
Last updated: 03 Apr 2023
About Author

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.

read less
  1. Share:
Salesforce Articles