Get a Free Consultation

Mastering Salesforce Apex Test Classes: A Comprehensive Guide

Mastering Salesforce Apex Test Classes: A Comprehensive Guide

Table of Contents


In the dynamic world of Salesforce development, crafting effective Apex Test Classes is paramount for ensuring the reliability, maintainability, and integrity of your code. This comprehensive guide delves deep into the fundamentals and best practices of crafting robust Apex Test Classes, empowering you to confidently navigate the Salesforce development landscape.

What is a Test Class?

In Salesforce development, a test class is a special class written in Apex that is dedicated to testing the functionality of your Apex code. Test classes are essential components of the Salesforce development lifecycle, designed to ensure that your code performs as expected under various conditions.

Understanding the Importance of Apex Test Classes

Disadvantages of Test Classes

  1. Time-Consuming – Writing comprehensive test classes can be time-consuming, impacting the development timeline.
  2. Test Data Management – Creating and managing test data can be challenging, especially when dealing with complex scenarios.
  3. Updates Required – As your code evolves, test classes need to be updated to reflect changes, adding to the maintenance overhead.

Creating Powerful Apex Test Classes: Key Best Practices

  1. @testSetup: Utilize this annotation to create test data once for the entire class, promoting efficiency and reducing redundancy.
  2. Bulk Data Handling: Design test methods to handle bulk data scenarios, realistically simulating real-world use cases.
  3. Positive and Negative Scenarios: Ensure comprehensive coverage by including both successful and exceptional edge cases in your test methods.
  4. Exception Handling: Employ the @isTest annotation to assert that the specified exception is thrown during test execution.
  5. Code Coverage: Aim for a code coverage goal exceeding 75% to guarantee thorough testing.
  6. Test Data Factory: Create a dedicated Test Data Factory for improved organization and reusability of test data.
  7. StartTest & StopTest: Use Test.StartTest and Test.StopTest within each developer’s test methods to ensure proper test context management.
  8. SeeAllData: Avoid using @IsTest(SeeAllData=true) unless absolutely necessary, as it bypasses data isolation and can impact performance.

Test Methods in Your Apex Test Class

The following are methods for Apex Tests Class. All methods are static.

Annotations In Apex Test Class

  1. @isTest
    1. Denotes that the class or method contains apex test class methods.
    2. Applied to the class or method to signal Salesforce apex test that it is a test class or method
  2. @testSetup
    1. Specifies methods that are used to create apex test records once for the entire test class.
    2. Applied to a method within a test class
  3. @testVisible
    1. Exposes private variables to test methods within the same class.
    2. Applied to variables that need to be accessed in test methods.
  4. @isTest(seeAllData=true)
    1. Allows tests to see data in the organization
    2. Applied to the test class or method when access to all data in the organization is required.
  5. @isTest(seeAllData=false)
    1. To ensure that your test methods are isolated from the existing data in the Salesforce environment.


Use of System Class Methods in Apex Test Class

We’ll explore the use of various assertion methods to validate and assert expected outcomes within your test classes.

  1. System.assert(condition, msg) – It checks whether a specified condition evaluates to true. If the condition is false, the assertion fails, and the specified message is logged.
  2. System.assertEquals(expected, actual, msg) – It is used to assert that two values are equal. If the values are not equal, the assertion fails.
  3. System.assertNotEquals(expected, actual, msg) –  asserts that two values are not equal. If the values are equal, the assertion fails.
  4. System.runAs(userSObject) – It is used to run the method in the user context.
  5. System.debug(msg) – It is Used to debug the code. we can debug code line by line.


public class ExampleTest {

    static void testWithAssertionsAndDebugging() {
        // Create test data
        Profile testProfile = TestDataFactory.createTestProfile(‘Test Profile’);
        User testUser = TestDataFactory.createTestUser(‘John’, ‘Doe’, ‘’, ‘johndoe’, testProfile.Id);

        // Perform the logic to be tested
        System.debug(‘### Starting Test: testWithAssertionsAndDebugging’);

        // Run a method in the user context
        System.runAs(testUser) {
            // Call a method that performs some logic
            Boolean result = MyLogicClass.processUser(testUser.Id);

            // Use System.assert() to check if the result is true
            System.assert(result, ‘The result should be true’);

            // Use System.assertEquals() to check if a specific value matches the expected value
            System.assertEquals(‘Expected Value’, MyLogicClass.getSomeValue(), ‘The value should be as expected’);

            // Use System.assertNotEquals() to check if two values are not equal
            System.assertNotEquals(‘Undesired Value’, MyLogicClass.getAnotherValue(), ‘The values should not be equal’);

            // Query the data after execution
            User queriedUser = [SELECT Id, FirstName FROM User WHERE Id = :testUser.Id];

            // Use System.assert() to verify the existence of the user
            System.assert(queriedUser != null, ‘The User should exist in the database’);

            // Use System.debug() to log information for debugging
            System.debug(‘### User Name after processing: ‘ + queriedUser.FirstName);

        System.debug(‘### Test completed successfully’);

How to Create Test Data In TestDataFactory

public class TestDataFactory {
    // Method to create a test profile
    public static Profile createTestProfile(String profileName) {
        Profile testProfile = new Profile(
            Name = profileName
            // Add other profile fields as needed
        insert testProfile;
        return testProfile;

    // Method to create a test permission set
    public static PermissionSet createTestPermissionSet(String permissionSetName) {
        PermissionSet testPermissionSet = new PermissionSet(
            Name = permissionSetName
            // Add other permission set fields as needed
        insert testPermissionSet;
        return testPermissionSet;

    // Method to create a test user
    public static User createTestUser(String firstName, String lastName, String email, String username, String profileId) {
        User testUser = new User(
            FirstName = firstName,
            LastName = lastName,
            Email = email,
            Username = username,
            ProfileId = profileId,
            Alias = ‘testU’,
            TimeZoneSidKey = ‘America/Los_Angeles’,
            LocaleSidKey = ‘en_US’,
            EmailEncodingKey = ‘UTF-8’,
            LanguageLocaleKey = ‘en_US’
        insert testUser;
        return testUser;

    // Method to create a test account
    public static Account createTestAccount(String accountName, String industry, String rating) {
        Account testAccount = new Account(
            Name = accountName,
            Industry = industry,
            Rating = rating
        insert testAccount;
        return testAccount;

    // Method to create a test contact associated with an account
    public static Contact createTestContact(String firstName, String lastName, String email, Id accountId) {
        Contact testContact = new Contact(
            FirstName = firstName,
            LastName = lastName,
            Email = email,
            AccountId = accountId
        insert testContact;
        return testContact;

    // Method to create a test opportunity associated with an account
    public static Opportunity createTestOpportunity(String opportunityName, Id accountId, String stageName, Date closeDate) {
        Opportunity testOpportunity = new Opportunity(
            Name = opportunityName,
            AccountId = accountId,
            StageName = stageName,
            CloseDate = closeDate
        insert testOpportunity;
        return testOpportunity;

    // Method to create a test case associated with a contact
    public static Case createTestCase(String caseSubject, String caseDescription, Id contactId) {
        Case testCase = new Case(
            Subject = caseSubject,
            Description = caseDescription,
            ContactId = contactId
        insert testCase;
        return testCase;

    // Method to create a test lead
    public static Lead createTestLead(String leadFirstName, String leadLastName, String leadEmail) {
        Lead testLead = new Lead(
            FirstName = leadFirstName,
            LastName = leadLastName,
            Email = leadEmail
        insert testLead;
        return testLead;

    // Method to create a test task associated with a contact
    public static Task createTestTask(String taskSubject, String taskDescription, Id contactId) {
        Task testTask = new Task(
            Subject = taskSubject,
            Description = taskDescription,
            WhoId = contactId
        insert testTask;
        return testTask;

How to use Test Data of TestDataFactory

public class MyTestClass {
    static void testData() {
        // Create test data for profiles and permission sets
        Profile testProfile =          TestDataFactory.createTestProfile(‘Test Profile’);
        PermissionSet testPermissionSet = TestDataFactory.createTestPermissionSet(‘Test Permission Set’);
        // Create test data for various standard objects
        User testUser = TestDataFactory.createTestUser(‘John’, ‘Doe’, ‘’, ‘johndoe’, ‘StandardUser’);
        Account testAccount = TestDataFactory.createTestAccount(‘Test Account’, ‘Technology’, ‘Hot’);
        Contact testContact = TestDataFactory.createTestContact(‘Jane’, ‘Doe’, ‘’, testAccount.Id);
        Opportunity testOpportunity = TestDataFactory.createTestOpportunity(‘Test Opportunity’, testAccount.Id, ‘Prospecting’,;
        Case testCase = TestDataFactory.createTestCase(‘Test Case’, ‘Case Description’, testContact.Id);
        Lead testLead = TestDataFactory.createTestLead(‘Lead’, ‘LastName’, ‘’);
        Task testTask = TestDataFactory.createTestTask(‘Test Task’, ‘Task Description’, testContact.Id);

        delete testProfile;
        delete testPermissionSet;
        delete testUser;
        delete testContact;
        delete testOpportunity;
        delete testCase;
        delete testLead;
        delete testTask;
        delete testAccount;

    // Add more test methods as needed


Test classes are the cornerstone of building reliable and maintainable Apex code in Salesforce. While they have their challenges, the benefits of increased code quality, compliance with Salesforce requirements, and protection against regressions far outweigh the disadvantages. By adhering to best practices and utilizing appropriate annotations, developers can create robust test classes that contribute to the success of their Salesforce applications.