Blog Blog Posts Business Management Process Analysis

How to Write Test Classes in Salesforce

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-


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.


Annotations of Test Class in Salesforce

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

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.

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.

Become a Salesforce Administrator and App Builder

Best Practices of Test Classes in Salesforce

Some rules to write Test Classes in Salesforce are-

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.

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

The post How to Write Test Classes in Salesforce appeared first on Intellipaat Blog.

Blog: Intellipaat - Blog

Leave a Comment

Get the BPI Web Feed

Using the HTML code below, you can display this Business Process Incubator page content with the current filter and sorting inside your web site for FREE.

Copy/Paste this code in your website html code:

<iframe src="" frameborder="0" scrolling="auto" width="100%" height="700">

Customizing your BPI Web Feed

You can click on the Get the BPI Web Feed link on any of our page to create the best possible feed for your site. Here are a few tips to customize your BPI Web Feed.

Customizing the Content Filter
On any page, you can add filter criteria using the MORE FILTERS interface:

Customizing the Content Filter

Customizing the Content Sorting
Clicking on the sorting options will also change the way your BPI Web Feed will be ordered on your site:

Get the BPI Web Feed

Some integration examples