How to Test Apex Classes Using Test Methods in Salesforce

While working as a Salesforce Developer in a consulting firm, I had to automate specific business processes for the Sales team. One of my tasks was to create an Apex class to automatically create a Contact record based on data provided by the Sales team during customer onboarding.

To verify the method of the apex class, I created a test class that will verify that the contact was created successfully with the correct name and email, and ensure that the record count increases after the class executes.

In this Salesforce tutorial, I will explain what Apex test classes are and how to Test Apex Classes Using Test Methods in Salesforce.

What are Apex test classes in Salesforce?

In Salesforce Apex, test classes are also created the way we create an Apex class, and we declare an Apex class as a test class using @isTest. The test classes in Apex ensure code quality by verifying potential issues in the test run.

The test class takes parameters of expected value and actual value using System.assertEquals(expected value, actual value). If both the values don’t match, the test case fails; otherwise, it passes.

Create Test Classes using Test Methods in Salesforce Apex

Before creating the test class, I will show the apex class that creates Contact object records using parameters such as first name, last name, and email, and then insert a new contact record into Salesforce.

Below is the code of the Apex class to create contacts with parameters first name, last name, and email.

public class ContactCreation {
    public static void createContact(String firstName, String lastName, String email) {
        Contact con = new Contact(
            FirstName = firstName,
            LastName = lastName,
            Email = email
        );
        insert con;
    }
}

Create the Apex Test Class:

Now, we will create the test class and need sample data to pass the class’s parameters for testing.

1. The method of creating a test class is the same as creating an Apex class by following File > New > Apex Class.

After entering the name of the test class, enter the test code below. This code will test the working of the ContactCreation class, and I have entered the code with reference to that.

@isTest
public class ContactCreationTest {
    @isTest
    static void testContactCreation() {

        String firstName = 'Adrian';
        String lastName = 'Smith';
        String email = 'adrian.s@mail.com';
        
        ContactCreation.createContact(firstName, lastName, email);

        Contact insertedContact = [SELECT Id, FirstName, LastName, Email FROM Contact WHERE Email = :email];
        
        System.assertEquals('Adrian', insertedContact.FirstName);
        System.assertEquals('Smith', insertedContact.LastName);
        System.assertEquals('adrian.s@mail.com', insertedContact.Email);
    }
}

In the above code, @isTest declares that the class is an Apex class and is only used to run tests in Salesforce Apex.

The System.assertEquals method checks if the expected value matches the actual value. If they don’t match, the test will fail.

2. After creating the test class, we have to run the test case, and for that, select Test > New Run.

3. Then, in the next window, select the test class name from the Test Classes column. After this, select the method of the test cases and click the Run button.

Run Apex test class in Salesforce

Now, after running the test class, click on the tests tab in the developer console, and there, you will see the timestamp when the test class is executed. It will also display the problems, if any occur.

How to run test classes in Salesforce Apex

The test class we created ran successfully, meaning the code works fine. This way, we can create and run the test classes in Salesforce Apex.

Conclusion

In this Salesforce tutorial, we learned to test an Apex class using the test methods in Salesforce Apex. Following the above steps, you can test your Apex classes using the various Apex class test methods and test your code before deploying it to the real-time environment.

You may also like to read.

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.