Blog

Testing APEX In Salesforce

  • (5.0)
  • | 909 Ratings
Testing APEX In Salesforce

Testing by code is called test class.

Test Class: When we write any business logic in the sandbox by APEX Class or Trigger, then we need to do testing of the same. The testing we do refers to manual testing by any QA guy. The only difference is, he/she will do manually and we will do by code - which is called a Test Class. This is something rare in other programming languages.

Code Percentage For Approval

The minimum code for approval for APEX class & Trigger is mentioned below.

APEX Class == 75%.
Trigger == 1%.

Industry Standards For Writing A Test Class

Main-Class Name: JeanDiscountClass.
Test Class Name: JeanDiscountClassTest.

Scenario: Test Class is for JeanDicountClass

Test Class Program For Testing: Before performing the test class execution, we need to create an Object and write a trigger class & APEX class function for inserting, updating, & deleting the data from the Object. 

The Above Test Class is for JeanDicountClass.

Program#1: 

@isTest // Called a Annotation
Public Class JeanClassDemonstartionTest{
    Static testMethod void MyTestFunction(){      
        //create new record data
Levis__c j = new Levis__c();
//same like, list MyList=New list();
j.Name = 'John';
j.Price__c = 200;
//Insert Jean
Insert j;
//how to retrieve the new Jean
Levis__c j2 = new Levis__c();
j2 = [SELECT Price__c FROM Levis__c WHERE id =: j.Id];
//test that the trigger correctly updated the price
system.assertEquals(180, j2.Price__c);
}
}

Please save the code properly, after “Save”(Ctrl + S), You will be able to visualize the “Run Test” button at the top right corner of the page as shown in the below figure. This button is available only for the Test Class, not for any regular class.

Testclassrun

Testclassrun 

Before pressing the Run Test Button, check with the main APEX Class - (JeanClassDemonstration).

The below figure shows the MainClass with code.

MainClass-JeanDemonstrationClass

MainClass-JeanDemonstrationClass

Check with the main class - trigger also, as shown in the figure below.

Trigger-mainclass

Trigger-Mainclass 

Note: At starting in the Trigger Class - Code Coverage is 0% and the APEX Class - Code Coverage is 0%, We need 75% code coverage from APEX Class and 1% from the Trigger Class. And, we must run the test from the Anonymous Window with - “JeanClassDemonstartionTest” and look for the test results.

Check Out Salesforce Tutorials

We can also run the test APEX class from the Setup → Develop → APEX Classes → Select the Desired Class to test(JeanClassDemonstartionTest).


testrun

testrun gif 

What and why the code should be passed with 75% and above to move for the production and why is that required?

For a clear explanation, we need to visit code from the Developer Console area. So, got to 

How to Open Trigger from Anonymous Window?

Developer Console → Anonymous Window → File → Cntrl + O or Open → Entity Type(Triggers) → Select the “Trigger”(JeanDiscountTrigger).

Jeandiscounttrigger

Jeandiscounttrigger

How to Open APEX Class from Anonymous Window?

Anonymous Window → File → (Cntrl + O) or Open → Entity Type(Triggers) → Select the “Classes”(JeanClassDemonstartion)

Jeandiscountclass

Jeandiscountclass

Frequently Asked Salesforce Interview Questions & Answers

In the above format, the required trigger, APEX Class, Test Class are opened and now, you must run the test in the Test Class Window by Clicking the “Run Test” button from the top right corner of the page.

Graphicalrepresentationtestclass

Graphicalrepresentationtestclass 

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

In the next topic, we will discuss in detail about “Test Class With Example 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

Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

Arogyalokesh
About The 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.


DMCA.com Protection Status

Close
Close