How to write Test Classes in Salesforce?
Updated on 22nd Sep, 21 23 Views

Salesforce allows the users to make their own test classes, approx 75% of the code is covered by the test classes themselves. This is done to make sure that there are no issues in Salesforce Dashboard during the production. Test classes allow testing the logic for Apex triggers, Apex class, Visual force Controller, Visual Force Extensions, Batch Apex, Queable Apex, and Future Apex.

Watch the video and learn all about Salesforce


What is Test Class in Salesforce?

The work of Test Classes in Salesforce is basically to check whether every logic is working as it has to or it could be positive or negative testing. Test classes are not counted in code coverage before it gets deployed by the Salesforce Admin in the final production.

Test Classes in Salesforce allow about 75% of code coverage, which means more than half of the testing work is completed by the test classes themselves. It works similar to Python testing where the code written is tested by a just-in-time compiler performing the testing.


Benefits of Test Classes in Salesforce-

These are some of the benefits of Test Classes in Salesforce-

  • Reduces the bug cost, i.e., it helps in troubleshooting
  • It performs bulk tests at a time.
  • It ensures desired output to the user.
  • High-quality apps are delivered to the production org, making production users more productive

How to create a Test Class in Salesforce?

These are the steps on how to create a Test Class in Salesforce-

Step 1 – At first, open the Salesforce dashboard

Step 2 – On the Quick Find tab, search Apex Classes

Step 3 – Click on New to select a new Apex Class

Step 4 – In this add the test class definition

Step 5 – This is the syntax –

private class MyTestClass {
    @isTest static void myTest() {
        // code_block

Step 6 – Your code will somewhat look like this depending on the object and trigger you have created in your Salesforce account.

This is one of the examples of Test Class in Salesforce.

Step 7 – You will be required to run this code in the Developer’s console and then put your custom-designed apex class code to the console.

Step 8 – Run this code to test your output in the console.

Step 9 – Select the Test Class which you want to run.

Step 10 – To add all methods in the Test Class to the test run, click Add Selected.

Step 11 – Click Run

Step 12 – The test result displays in the Tests tab. Optionally, you can expand the test class in the Tests tab to view which methods were run. In this case, the class contains only one test method.

Your Test Class is created

Practicing for Salesforce interview? Check out our Top Salesforce Interview Questions & Answers.

Get 50% Hike!

Master Most in Demand Skills Now !


Annotations of Test Class in Salesforce

These are some of the annotations used in a Test Class in Salesforce-

  • @isTest – It is used at the class or method level to indicate that it contains only test coverage code. Classes defined with this annotation do not count your organization’s Apex code limits.
  • @testSetup – Indicates the specific method used to set the test data. If @testSetup is specified, it will be executed before any other methods in the test class. Regardless of how other test methods use the data, each test method can access the original generated test data set.
  • @testVisible – When creating Apex logic, it makes sense to define elements such as methods, variables, and internal classes as private or protected.

However, this can make test coverage difficult. Fortunately, Salesforce took care of the future and provided us with @testVisible annotations.

Using this annotation on private or protected members allows access to test classes, but maintains the visibility defined for non-test classes.

  • @isTest(See all Data=True) – Salesforce test class should be responsible for creating its own data. However, sometimes you need to access existing data, and using @isTest (SeeAllData = True) can provide access to your test classes and test methods.

Available since API 24.0, there are some precautions for using it. SeeAllData = True can be used at the class and method level. When SeeAllData = True at the class level, all methods can access existing data, but when SeeAllData = True at the method level, these methods can only access existing data.

Finally, using SeeAllData = False at the method level will not override the SeeAllData = True used at the class level.

  • @isTest(isParallel = True)- Use the @isTest(isParallel=true) annotation to point take a look at categories that may run in parallel. Default limits on the amount of coinciding tests don’t apply to those test classes.
Become a Salesforce Administrator and App Builder

Best Practices of Test Classes in Salesforce

Some rules to write Test Classes in Salesforce are-

  • Always start with @is Test annotations to make Salesforce understand that we are writing a Test Class
  • Always try to keep the class private, try to name it as the original class or trigger name.
  • Methods of your test class have to be static, void and testMethod keywords have to be used.
  • Use Test.startTest() and Test.stopTest () to ensure that the actual testing of the code is done using a new set of governor constraints. These methods can help you reset the governor limits before running the test code.
  • Once your test code runs between Test.startTest() and Test.stopTest(), you need to use assert statements to check whether or not your actual code is executed properly and give the results as expected.

In our case, we tend to test whether the book’s worth has been set to ninety or not. If this assert statement returns false, then your test category will fail and will let you know that one thing isn’t correct in your code, and you should repair your original code.

  • When a deadlock occurs in tests that are running in parallel, they try to create records with duplicate index field values. A deadlock occurs when two running tests are waiting for each other to roll back data. Such a wait can happen if two tests insert records with the same unique index field values in different orders.

Have any more queries regarding Salesforce Developer Jobs, Join our Salesforce Community page and get your queries answered.

Course Schedule

Name Date
Salesforce Certification 2021-10-30 2021-10-31
(Sat-Sun) Weekend batch
View Details
Salesforce Certification 2021-11-06 2021-11-07
(Sat-Sun) Weekend batch
View Details
Salesforce Certification 2021-11-13 2021-11-14
(Sat-Sun) Weekend batch
View Details

Leave a Reply

Your email address will not be published. Required fields are marked *

Looking for 50% Salary Hike ?

Speak to our course Advisor Now !

Associated Courses

Subscribe to our newsletter

Signup for our weekly newsletter to get the latest news, updates and amazing offers delivered directly in your inbox.