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.
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?
How to find the amount of code covered in APEX Class?
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.
How to Highlight the Code that is executed?
Now, open the APEX class → “Code Coverage” and press the downward arrow button → Select “All Tests 100%”
Subscribe to our youtube channel to get new updates..!
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).
How to find the amount of code covered in APEX Trigger?
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”.
How actual calculation is done in PROD(Salesforce Org)?
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