Test Classes in Salesforce - Learn All About it Here
Content
What is a Test class in Salesforce? |
What is the need of a Test class in Salesforce? |
How to write Test class in Salesforce? |
What is the syntax of the Test class with example? |
Best Practices of Test classes in Salesforce? |
What is Test Class in Salesforce?
Ans: - In Salesforce, if you want to deploy your package (or release your package or launch your package) in production then you have must important to complete to the code coverage at least 75% of your apex code and triggers.
For completing your code coverage, you need to write a test class for your apex code.
What is the Need of a Test Class in Salesforce?
Ans: - Test Classes ensure high quality for your Apex code and let you meet requirements for deploying Apex. And test classes check to the functionality of your apex code that’s meet to the client requirements.
How to write Test class in Salesforce?
First, log in to your developer Salesforce account then go to setup and try to find Apex classes in Quick Find Box, then select Apex Classes within custom code.
After selecting Apex class, click on New and write your code.
Don't forget to check out: 10 Example Future Method with Test Class | Asynchronous Apex | Salesforce Development Training Video
After writing your code, click on save and save with LeadProcessorTest
To run this test class and view the code coverage, you need to go to the developer console and click on Test -> New Run -> Select your test class (LeadProcessorTest) -> Run
What is the Syntax of the Test Class with Example?
Batch Class LeadProcessor
Test Class of LeadProcessor
Check out another amazing blog by Abhishek here: All You Need to Know about Batch Apex in Salesforce
Best Practices of Test Classes in Salesforce?
A developer must ensure that the following points are included in the test class while writing effective test cases.
- The @isTest annotation must appear at the top of the test class.
- Focus 90+: At least 75% code coverage is required for production deployment. However, try to concentrate on 90% or more of the time. We should not be concerned with the percentage of code coverage; instead, we should ensure that all use cases, including positive, negative, bulk, and single record, are covered.
- Each method has one Assert Statement: For both negative and positive tests, always include assert statements.
- Create test records once in a method and use in all test methods in the test class with @testSetup.
- Create a TestFactory class with the @is annotation.
- Because Apex test class runs in system mode, the permissions and record sharing are ignored. To enforce record sharing, we must use System.runAs.
- Hard Coding Ids should not be used anywhere in the test class or any apex class.
- Using the Test.startTest() provides developers with a new set of governor limits for their test's act stage. They can then revert to their old governor restrictions using Test.stopTest(). Limits Class must be used to test for Governor Limits in test classes.
Responses