You are currently viewing Overview of Test Class in Salesforce

Overview of Test Class in Salesforce

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

  1. Guarantees that your Apex class and triggers function as expected.
  2. 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.
  3. Meet the code coverage requirements for deploying Apex to production or distribute Apex to customers through packages.
  4. High-quality applications delivered to the production org that makes the users more productive.
  5. 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 Method:

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.

@isTest

    private class MyTestClass {

        @isTest static void myTest() {

            // code_block

        }

    }

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.

Conclusion

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.

Akshay

Akshay Dhiman

Chief Technical Officer (CTO)

“Akshay Dhiman, the CTO of Cloud Analogy, has been a standout and successful Salesforce Platform Developer for years. He has a rich experience in Salesforce Integration, JavaScript, APEX, VisualForce, Force.com Sites, Batch Processing, Lightning, PHP, C++, Java, NodeJs, ReactJs, Angular 8, GraphQL, React Native, Web Technology, and jQuery. Known for his problem-solving and debugging skills, Akshay is an out-of-the-box thinker and his capability to understand the business context and translate it into a working model is par excellence. Akshay would not only translate his thoughts into reality but would also bring in his own perspective that is always a tremendous value add. Akshay has the knack of taking challenges head on, equipped with In-depth industry knowledge, Resourcefulness and uncanny nag to build relationship with anyone in shortest time possible. Not only does he possesses fantastic technical depth and awareness but Akshay also complements them with a profound understanding of business functionalities, tools, and methodologies. He has the rare combination of skills and talent that one looks for in Salesforce – attention to detail and the drive for innovation.”

Leave a Reply