Best Practices For Testing Apex in Salesforce Testing is very important for an organization to set up successful software development: here are some general guidelines (and a code sample) to develop your Apex testing in Salesforce

Best Practices For Testing Apex in Salesforce

Testing has great importance in the Software Development Life Cycle (SDLC). Testing is very important for an organization to set up successful software development.

There are numerous reasons behind why we need to test code – it helps identify all the bugs in the code, it uncovers what else is required, it checks whether or not the code is performing well and delivering the expected results, and last but not the least it aids in developing bug-free software.

How Apex Testing matter in Salesforce development?

Apex Testing is very important in Salesforce. Developers use developer edition or sandbox to write Apex code. Before deploying this code to the production, testing is very important as developing any apex code in the production is not possible, we need to test first to check if it is working correctly with Salesforce API.

What is a Test Class?

After writing Apex code in sandbox or developer edition, you need to test it before deploying to prod. You may be thinking that I am talking about manual testing means a QA will check the whole code manually. No, here the difference is that testing will be done by using code, it is called Test Class. In simple terms, Test Class is when we use coding to perform the Apex testing task. This is very uncommon in other programming languages as compared to Salesforce development.

Reason for writing Test Class

Before starting the procedure of writing the Apex test in Salesforce, we need to understand the reason and importance of writing Test Class. To check that code for a flow or process is bug-free or it is executing as expected, we need to perform Unit testing. We write test classes to perform these unit tests.

What is code coverage?

Testing is done for code coverage. Do you know what do I mean by code coverage? Code coverage is the total number of lines that are covered under the testing process. Code coverage should be a minimum of 75% before deploying it from developer edition or sandbox to production. However, the best practice is to focus on 100% of the code and testing the whole through test class to check all positive and negative of it. And, while using triggers in your code make sure its coverage must be at least 1%.

Best Practices For Testing Apex in Salesforce

All about Salesforce Apex Unit Testing

To perform the unit testing of classes and triggers, salesforce provides Apex framework, where you can write the test, run it, and check whether or not all the functionalities of code are working correctly. This testing also gives the result of code coverage means how many lines in classes and triggers have been tested by the test method in the Apex test class. We test class in Salesforce, and no test data is saved in the database. Checking code in the test class is important for Salesforce because it ensures that code will not break at all after being deployed to production.

Let’s discuss some key points related to the test class for Apex code in Salesforce and understand how to write a test class.

  • @isTest annotation

When you start writing test class, you should start it with @isTest annotation so that Salesforce can identify it as a test class. This annotation is used to declare a class as a test class. By using this annotation, we set a code limit.

  • Raw data

You cannot access data stored in Salesforce org directly from test class due to default settings. To access the storage, you need to create raw data in your test class. Add SeeAllData=true to @isTest annotation means it will look like @isTest (SeeAllData=true). By writing so, you will be able to access all the data stored in Salesforce org.

  • debug

System.debug is not considered as part of code coverage.

  • No limitation on writing test classes

Test classes and test methods don’t fall under the category of apex code limitations. Hence, there is no boundary while you are writing a test class with multiple methods, all you need to keep in mind is that each part of the code must be covered.

  • Test class naming

The best way to name the test class is to add ‘Test’ to the original class or trigger and keep the class private. For example, if a class name is ‘ArticleCount’ then its test class name should be ‘ArticleCountTest’ and overall syntax will be ‘Private Class ArticleCountTest’.

  • Test method naming

Test class methods are always static, void and a keyword testMethod is always used. For example, if method is ‘validateArticleCount’, it should be written as ‘static testMethod void validateArticleCount()’.

  • Use governor limits

You need to use governor limits - Test.startTest() and Test.stopTest() first and then start executing the test class to check the code. These limits can be reset but only before the execution of the apex test class.

  • Use of assert statements

After setting up the governor limits, you need to use an assert statement to check whether or not the main code is performing as expected. If the assert statement returns false value, the test will automatically get failed and will instruct you to check the original code & fix the bugs.

  • Write positive and negative test cases

Most people write only positive test cases but, in an ideal situation, a test class is considered right when it contains multiple test methods to test positive as well as negative use cases.

How to write a Test Class

  • Scenario – Test class for ArticleDiscount
  • Main class – ArticleDiscount
  • Test class – ArticleDiscountTest

Source Code

Press CTRL+S to save the complete code and then click Run Test button to run the test class. You will see this button only for the Test Class no other classes. Before running the code check the main Apex class and Apex trigger, and while testing make sure the minimum code coverage for main class is 75% and for trigger class is 1%.


Before writing the test class and test methods, make sure you are aware of the correct syntax. The above information is very helpful in writing a test class. Try to cover the 100% code to be covered while performing unit testing through Apex because it is considered as best practice. You can choose a test name as per your choice under certain limitations but it is great to mention the main class name in the test case. I hope you have information now to start writing a test class in Apex Salesforce!


Digvijav Upadhyay

About Digvijav Upadhyay

Digvijay Upadhyay has over 7+ years of experience in Technology content writing at JanBask Training. Digvijay is specialized in cloud computing, Salesforce, digital marketing, technology trends and write other valuable IT topics for online audience. Find me on Linkedin.

View all posts by Digvijav Upadhyay

Leave a Reply

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

The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.