Testing APEX In Salesforce

Testing is an incorporated portion of the Apex, or in any software development. In Apex, we will have individual test classes for developing unit test cases. Testing is vital for successful software development. Several experts suggest that we should use a test-driven development process. The code developed should be tested instantly. So, those errors can be minimized.

Want to enhance your skills in dealing with the worlds best CRM, enroll in our "Salesforce Training". 

Apex Testing

Need for Apex Testing

If you are developing a software product for end-users, then testing is the key aspect of the success of that product. Testing is used to test the given product, whether it is working according to the expectations or not. Two methods for testing an application: one way of testing is with the help of the salesforce user interface. 

Testing through the salesforce user interface makes the product testing from different perspectives. Another way of testing is testing bulk services, and you can send 200 records with the help of this code if you called through SOAP API. To implement the applications for salesforce App exchange, the following rules should be satisfied.

Observe the following:

  • Unit tests must conceal a minimum of three-by-fourth of our code; all these tests must be completed efficiently.
  • While implementing the application in a production enterprise, every unit test in the enterprise namespace is implemented automatically.
  • Invoking system.debug is not considered as a component of apex code.
  • Test classes and Test methods are not considered as a component of apex code.
  • Each trigger should have a precise testing scope.
  • All of the triggers and classes must be compiled successfully.

What is Tested in Apex

Salesforce develops tests for the following:

1. Single Action

It is tested to check whether an individual record generates the appropriate or expected result.

2. Bulk Actions

Any Apex code like a class or a trigger or extension can be called for 1 to 200 records. You should test individual or Bulk records.

3. Positive Behaviour

It is tested to check whether the assumed behavior happens in every assumed mutation.

4. Negative Behaviour

This behavior is considered as a restriction to the applications like cannot indicate a negative amount, cannot include future data, etc. 

5. Restricted user

It is used to test whether the user implements the code or gets error messages.

 MindMajix YouTube Channel

Apex Unit Test

To ease the development of strong and flawless code, Apex endorses the execution and creation of unit tests. Unit tests are considered as class methods that test whether a specific part of code is working accurately. Methods of Unit tests will have no arguments, will not save data to the database. Unit tests should be specified in the test classes, that are written with “@isTest”.

Unit Tests Example


Private class mindmajix {

Static void testing1() {

Program statements



Private class Mindmajix


@istest static void test_1

  Test code to be executed.


@istest static void test_2


Test code to be executed.


Test Data

Apex test data is temporary and not saved in the database. After executing the test method, the inserted data is not saved in the database. All the modifications done to the available records are not saved or committed in the database. The temporary behavior of test data eases data management.

Running unit tests

To verify the utility of the apex code, we will run the unit tests. We will run the apex test methods in the developer console.

We may run the following groupings of test cases:

  1. Few or total all test methods in a particular class.
  2. Few or full methods in a group of classes.
  3. An inbuilt collection of classes called a test suite.
  4. Complete unit tests in the organization.

We can run the unit tests through the following:

  1. Salesforce user interface
  2. Salesforce expansions through visual studio code.
  3. API
  4. Lightning Platform Developer Console.

Running Unit tests through the Salesforce Interface:

Apex Test Excution

Frequently Asked Salesforce Interview Questions & Answers

Steps for Running Unit Tests are as follows:

1. In the Setup, type “Apex Test Execution” in the “Quick Find Box”, then choose Apex Test Execution.

2. Press “select Tests”.

3. Choose the tests to run. The catalogue of tests contains simply classes that comprise test methods:

  • To choose a test within an installed package, choose a namespace of a managed package from the fold-out list.
  • To choose a test that is present at the local level of your enterprise, choose [My Namespace] from the fold-out list.
  • To choose a test, select [All Namespaces] from the fold-out list.

4. To withdraw from gathering code coverage data while running tests, choose “Skip Code Coverage”.

5. Press Run.


In every software development, testing plays a crucial role to make it successful. In Apex also, testing is necessary to test the functionality of the product. In Apex we will check the behavior and actions of the product. We will have specific methods and classes to implement unit tests. Unit tests make the code error-free. In this way, we will test the apex code to develop a successful software product.

Are you looking to get trained on Salesforce, we have the right course designed according to your needs. Our expert trainers help you gain the essential knowledge required for the latest industry needs. Join our Salesforce Certification Training program from your nearest city.

Salesforce Training Chennai, Salesforce Training Dallas, Salesforce Training Bangalore, Salesforce Training Hyderabad, Salesforce Training Mumbai, Salesforce Training Delhi, Salesforce Training Noida, Salesforce Training New York, Salesforce Training Chicago, Salesforce Training Kolkata, Salesforce Training Gurgaon, Salesforce Training Pune.

These courses are equipped with Live Instructor-Led Training, Industry Use cases, and hands-on live projects. Additionally, you get access to Free Mock Interviews, Job and Certification Assistance by Certified Salesforce Trainers

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

In the next topic, we will discuss in detail about “Test Class With Example In Salesforce”. Keep following us for more info on Salesforce Development / Programming.

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Salesforce Administration Training Salesforce Lightning Training 
Salesforce Advanced Developer Training Salesforce Developer Training 
Salesforce IoT Training Salesforce App Builder Certification Training 
Salesforce AppExchange Training Salesforce Service Cloud Training 
and many more.  
Course Schedule
Salesforce TrainingJun 15 to Jun 30View Details
Salesforce TrainingJun 18 to Jul 03View Details
Salesforce TrainingJun 22 to Jul 07View Details
Salesforce TrainingJun 25 to Jul 10View Details
Last updated: 03 Apr 2023
About Author

Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.

read less
  1. Share:
Salesforce Articles