Test classes in Salesforce

Test classes in Salesforce

What is Test Class in Salesforce?

The purpose of test classes in Salesforce is to ensure that all logic functions appropriately, which can be either Whether a test is positive or negative. Therefore, test classes are not included in the code coverage calculation. Instead, they are deployed in final Production by the Salesforce administrator.

Salesforce test classes allow for approximately 75% code coverage, meaning that the test classes perform more than half of the testing work. Salesforce has done this to ensure that our code does not fail in any circumstances in Production.

Some facts about Salesforce test classes that you should be aware of:

    • Unit tests must cover at least 75% of your Apex code, and all those tests must pass. However, this should not be our primary focus. Instead, we should strive for 100% code coverage, covering and testing every branch of your code for positive and negative use cases.
    • Apex code coverage does not include calls to System. Debug.
    • Test methods and classes are not included in the Apex code limit. So there’s no need to write a lengthy test class with numerous ways to ensure that all of your code branches are covered.
    • Every class/trigger can be deployed with 0%/1% coverage as well. Still, as I mentioned in the previous point, the broad range of your production org after your code is deployed should be 75% or higher, or Salesforce will not let you deploy your code.

Advantages of Test Classes:

    • High-quality apps are delivered to the production organization, making production users more productive.
    • Package subscribers receive high-quality apps, which increases customer trust.
    • Bug elimination Cost
    • Carry out bulk tests


    • @isTest – used at the Class or Method level to specify that the code only supports Test Coverage.
    • @testSetup is used to specify that a method is only used to set test data. When the @testSetup form is supplied, it is executed before other ways in the test class. Each test method will have access to the original set of constructed test data, regardless of how any other test method uses that data.
    • @testVisible -When building Apex logic, it’s a good idea to mark members like methods, variables, and inner classes as private or protected. However, doing so may make achieving Test Coverage more difficult. When applied to personal or protected members, this annotation permits test class access while preserving non-test class visibility.

Methods used in Salesforce Test Classes:

StartTest() – Marks the point in your test code when your Test begins. When testing governor limits, use this method.

StopTest() – Defines the point in your test code where your Test will end. This method should be used in conjunction with the most intelligent approach.

Must unit tests be completed?

In Salesforce, we must create test classes for the following items/components.

    • Apex Trigger
    • Handler/Helper
    • Apex Class
    • WebService
    • Apex REST/SOAP
    • VF Page
    • Apex Batch/Queueable/Future Method 
    • Custom Controller

Where Should You Write Test Class?

To write test classes, you can use the tools listed below.

    • Developer console
    • Setup -> Apex Classes -> Create/Open Existing Test Class in Salesforce UI
    • The code is written in Visual Studio
    • Any IDE tool will do.

How can I make a Salesforce test class?

The steps to create a test class in Salesforce are as follows:

Step 1 – Launch Salesforce first.

Step 2: Type “Apex Classes” into the Quick Find tab.

Step 3: To choose a new Apex Class, click on New.

Step 4: Include the definition of the test class here.

Sample Test Class:

Apex Trigger

trigger SampleTrigger on Car__c (before insert) {

Bike__c[] Bikes = Trigger.new;



Apex Class:

public class SampleApex{

public static void discount(Bike__c[] Bikes) {

for(Bike__cc :Bikes){

b.Price__c *= 0.7;




Apex Test Class:


private class SampleApexTest{

static testMethod void validatesSampleApex() {

Bike__c b = new Bike__c(Name=’royal and field’, Price__c=150000);


insert b;


// Retrieve the record

d = [SELECT Price__c FROM Bike__c WHERE Id =:d.Id];

//Test that the trigger correctly updated the price

System.assertEquals(80000, d.Price__c);



Best Practices in Salesforce Test Class:

    1. If the class version exceeds 25, the test class must begin with the @isTest annotation.
    2. The test environment also supports @testVisible and @testSetUp.
    3. A unit test is used to determine whether or not a specific piece of code is working correctly.
    4. The testMethod keyword indicates that the unit test method takes no arguments, commits no data to the database, and sends no email.
    5. At least 75% code coverage is required for production deployment.
    6. @isTest should be added to the test class.
    7. The @isTest annotation with the test method is the same as the testMethod keyword.
    8. There should be no void return type in the test method.
    9. Regardless of whether an access specifier is added, test class and method default access are private.
    10. Classes marked with the @isTest annotation cannot be interfaces or enums.


A type of software testing called UNIT TESTING involves testing single program modules or components.

You can see that unit testing can be complicated. Depending on the application being tested and the testing methods, tools, and philosophies used, it can be complex. That was always going to happen.

Article Name
Test classes in Salesforce
Test classes in Salesforce that all logic functions can be either a test positive or negative. They are deployed in final production by the SF administrator.
Publisher Name
BugendaiTech Pvt Ltd
Publisher Logo
Spread the updates

Leave a reply