The Apex Testing Framework lets you write and run tests for your Apex classes and triggers on the Lightning platform. Apex unit tests ensure the high-quality of your Apex code and allow you to meet the requirements for implementing Apex.
Testing is the key to successful long-term development and an important part of the development process. The Apex test framework makes it easy for you to test your Apex code.
The Apex code can only be written in the sandbox or a developer org, not in production. Apex Code can be deployed to a production org from a sandbox. Application developers can also distribute Apex code to customers from their Developer orgs by uploading packages to the AppExchange Salesforce Lightning Platform.
Apex component testing is very important not only for quality assurance but also for implementing and distributing Apex.
Benefits of Apex Testing
- Guarantees that your Apex class and triggers function as expected.
- A series of regression tests that can be repeated every time. Classes and triggers are updated to ensure that future updates to your application will not affect the existing functions.
- Meet the code coverage requirements for deploying Apex to production or distribute Apex to customers through packages.
- High-quality applications delivered to the production org that makes the users more productive.
- The high-quality application offered to package subscribers increases the trust of your customers.
Code Coverage Requirement for Deployment
Before you can use the code or package it for the AppExchange Lightning Platform, at least 75 percent of the Apex code must be tested and all of these tests must be passed. In addition, each trigger must have some scope. Even though code coverage is an implementation requirement, don’t write tests only to meet these requirements. Be sure to test common use cases in your application, including positive and negative test cases, and single-record processing.
Test methods must be defined in test classes that are classes annotated with isTest. This sample class shows a definition of a test class with one test method.
Test classes can be either private or public. If you’re using a test class for unit testing only, declare it as private. Public test classes are typically used for test data factory classes.
Salesforce Best Practice for Test Classes
- From deploying to production, it requires a minimum code coverage of 75 percent.
- The test class must start with @isTest annotation if the class version is more than 25.
- @isTest annotation with the testing method equals to the testMethod keyword.
- Test class and method default access are private, there is no access specifier that needs to be added.
- The @isTest annotation classes cannot be an interface or enum.
- The test method code cannot be called by non-test requests.
- The testing method does not allow the Salesforce API to be stored in non-test classes.
- Always use the @testSetup method to create test records for this class.
- If possible, don’t use seeAllData = true. Create your own test data. SeeAllData = true does not work with API 23 versions earlier.
- Users, Profiles, Organizations, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent, ApexPage can be accessed without(see AllData = true).
- You can use the @TestVisible annotation to access private members and methods inside the test class.
- Test methods and test classes are not considered as a part of the code limit.
- The testing method does not takes arguments, does not commit data to the database, and does not send emails with the testMethod keyword.
- Use Test.startTest () to reset the governor limits in Test methods.
- If you are performing an asynchronous code operation, you must call Test.stopTest () to ensure that the operation is completed.
- Use the System.runAs () method to create tests and profiles related to OWD. This is very important for security reasons.
- System.runAs () does not require user permission or field level permission
- Use as many assertions as System.AssertEquals or System.AssertNotEquals.
- Always test the Batch Capabilities of your code by sending 20 to 100 entries.
- Always try to pass a null value for each method. This is the area where most of the programs fail unnoticedly.
- Use call layout mocks to test web-service call out.
- You can perform unit testing using the standard Salesforce user interface, Force.com IDE, console, and API.
Salesforce records created using test methods are not included in the database. They return when the test finished execution. This behavior is convenient for testing because you don’t need to clean the test data after the test executes.
By default, Apex tests do not have access to existing credentials in the org, except for access to setup and metadata objects, such as User or Profile objects. Setup the test data for your tests. Creating test data makes your tests more powerful and prevents failures that are caused by missing or changed data in the org. You can create test data directly in your test method or by using a utility test class.
Chief Technical Officer (CTO)