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:
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:
Trigger Helper Program:
Test Class Program:
@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.
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
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:
Name | Dates | |
---|---|---|
Salesforce Training | Nov 19 to Dec 04 | View Details |
Salesforce Training | Nov 23 to Dec 08 | View Details |
Salesforce Training | Nov 26 to Dec 11 | View Details |
Salesforce Training | Nov 30 to Dec 15 | View Details |
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.