Apex Testing in Salesforce
The Apex Testing enables us to write and execute tests for the Apex Code. An important phase of the Salesforce development life cycle is Testing. The Apex Testing makes easy to test Apex Code. Apex Code can be written only in the Sandbox environment or Developer Console in production. Salesforce demands that at least 75% of the code be covered by Test Classes before deploying the built code into the production environment. In order to prevent our code from breaking in Production, Salesforce made the change.
- @isTest: It is used to specify that a class or method only contains code that provides test coverage. The organisation limit for Apex code does not apply to classes defined with this annotation.
- @testSetup: It is used to indicate that a method is used only to create test data. The @testSetup method, if provided, is called before all other methods in the test class. Regardless of how any other test method uses the original set of created test data, each test method will still have access to it.
- @testVisible: Defining members like methods, variables, and inner classes as private or protected makes sense when creating Apex logic. However, doing so might make it harder to achieve Test Coverage. Salesforce foresaw our need and gave us the @testVisible annotation. While maintaining the defined visibility to non-test classes, using this annotation on private or protected members permits test class access.
- @isTest(SeeAllData=True): Your Salesforce test classes should be responsible for the charge of generating their own data, ideally. Using @isTest(SeeAllData=True) enables your test classes and test methods access to existing data, which is sometimes necessary. At the Class and Method levels, "SeeAllData=True" can be used. All methods have access to existing data when "SeeAllData=True" is used at the Class level; however, when used at the Method level alone, only those Methods have access to existing data. The usage of "SeeAllData=False" at the Method level will not override the use of "SeeAllData=True" at the Class level.
- @isTest(isParallel=true) : To identify test classes that support parallel execution, use the @isTest(isParallel=true) annotation.
Don't forget to check out: Apex Basics and Database | Salesforce Apex Tutorial Guide
Methods in Salesforce Test Classes
Test.startTest() and Test.stopTest()
- startTest() - The startTest Method marks the point in the test code when the test actually begins. Each test method is permitted to call the startTest method only once.
Example - public static Void startTest()
- stopTest() - The stopTest Method marks the point in the test code when the test ends. We can use conjunction with startTest Method.
Example - public static Void stopTest()
How we can create Test Classes in Salesforce?
- Data Creation: You must first build the test class's data. Since this test class doesn't normally have access to the organization's data, you must grant it access. The test class can access the organisation data if you set @isTest(seeAllData= True).
- Annotation: The @isTest annotation is required to indicate that the test class will not count toward the organization's overall code limit.
- Starting and Stopping Test Methods: Using the Test.startTest() and Test.stopTest() classes, the test classes can be handled. These classes have an event or action that both starts and stops test classes.
- assert(): This method is employed to compare the output that is produced with the result that was assumed.
Where we Can write Test Class?
- Developer Console.
- Salesforce UI : Setup > Apex Classes > Create/Open Existing Test Class.
- Visual Studio Code.
Check out an amazing Salesforce video tutorial here: Salesforce Apex Tutorial for beginners | Apex Salesforce Tutorial
Unit Tests to be Done for?
- Apex Triggers
- Apex Classes
- Visualforce Pages
- Batchable Apex