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.
Percentage of code coverage: For the code to be moved to the production, the code coverage for APEX Class - 75%,
Trigger Class - 1%
Code Coverage: How many lines we are covering by our Test Class, i.e., called Code Coverage in Salesforce?
Open the Test Class in the Developer Console → Anonymous Window → File → Open → Test Class → Click on “Run Test” button → Open bottom of the window → Open the “Tests” Tab.
Now, open the APEX class → “Code Coverage” and press the downward arrow button → Select “All Tests 100%”
You can find some of the code highlighted with blue color on the screen. It shows the code executed for the “Test Class”.
Now, let us look into deeper concepts, we will change the business logic in the APEX Class and look into the changes occurs.
Let us include different business logics with different discount in the APEX Class, and look for the code coverage changes.
Now, run the test from the “Test Class”, and look for the changes that occur. And now, open the APEX Class program and Highlight the code in the previous process (How to Highlight the Code that is executed?).
In the above image, partial code is highlighted with blue color as it is executed, and the remaining in red in color as it is not executed because of the value inserted into the DataBase via the program. The amount of code executed is 40%, so the code cannot be moved to Salesforce Production Environment. This is because it should reach a minimum of 75% to get into the production environment.
Now, we will try to include different functions in the “Test Class” for the purpose of data insertion with different values and look for the Code Coverage percentage and the code execution from “Test Class” → Run Test.
In the above code, 60% of the code is executed, and still, it is not possible to move to PROD (Salesforce Org). Now, we will look into the total code coverage with all the possible functions in the “Test Class” -- Run Test.
In the above code, 100% of the code is executed, and now it is possible to move to PROD(Salesforce Org).
As discussed in the previous article, not only APEX Code but also the Trigger Code must be above 1% or else the code will not be moved to the PROD(Salesforce Org). So, we will have a look at the code highlighted and code coverage percentage in “Trigger Class”.
The above figure indicates the code coverage of “Trigger Class”.
It can be explained with the below example values. Let us assume that we are sending the below files to the PROD with the code coverage values like,
TestClass1 ApexClass1 -- 75% TestClass2 ApexClass2 -- 50% TestClass3 ApexClass3 -- 80% The average of all the three classes will be taken, 75 + 50 + 80 = 205/3 = 68%
ERROR! These class won't be sent to PROD(Salesforce Org).
TestClass1 ApexClass1 -- 90% TestClass2 ApexClass2 -- 50% TestClass3 ApexClass3 -- 90% The average of all the three classes will be taken, 90 + 50 + 90 = 230/3 = 76%
Salesforce will send all the code to PROD(Salesforce Org).
Note: The average of classes in the package will be taken into consideration by Salesforce to move the content or code into the PROD(Salesforce Org).
In the next topic, we will discuss in detail about “SOQL Of 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
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.