What is Salesforce Testing?

Salesforce testing is the process of evaluating the functionality, quality, and performance of Salesforce applications or customizations. It involves testing Salesforce workflows, triggers, validation rules, custom objects, reports, and integrations, to ensure they all work as intended and meet the business requirements.

Why Do We Need Salesforce Testing?

Salesforce is a highly versatile and comprehensive platform, enabling businesses to easily customize it for their specific requirements. It provides a wide variety of tools, resources, as well as a vast ecosystem of third-party integrations on the AppExchange marketplace to help businesses tailor the platform to their needs. In such a complex system, conflicts can easily arise. For example:

  • When upgrading Salesforce to a newer version, it can conflict with the existing customizations
  • New external systems, APIs, and integrations may clash with already installed integrations
  • New data validation rules can be too strict or inconsistent with existing data, causing issues with data entry and updates
  • Heavy data processing can impact performance
  • Customizations that modify user access controls can lead to security issues

With thorough testing, the QA team can pinpoint areas of conflict and resolve quickly before release, mitigating negative impact of bugs to the bottom line. In addition to the benefits of checking the customizations, Salesforce testing brings the same benefits as conducting general software testing, including:

  • Ensure system reliability and stability
  • Minimize risk of system failures, data loss, or performance issues
  • Maintain data integrity in the system
  • Improve user experience by identifying and eliminating friction
  • Ensure compliance and security

Types Of Salesforce Testing

When performing Salesforce testing, just like any other type of testing, there are 2 major approaches:

  • Manual Testing: Testers evaluate the interactions between Salesforce modules and integrations by manually interacting with them, without the help from automated testing tools or test scripts. 
  • Automated Testing: Testers leverage automation frameworks or adopt automation testing tools to execute Salesforce test scripts automatically, at scale

Test Class Structure

A test class typically contains test methods that exercise the behavior of the methods and logic in your Apex classes and triggers. These test methods replicate different scenarios to validate the correctness of your code.

Test Class Methods

Test methods are annotated with @isTest to indicate that they are test methods. Test methods can be either public or private based on their intended use and purpose within the test class. Here’s a breakdown of when to use public or private test methods:

Public Test Methods

When to Use:

  • Invoking from Outside the Test Class: Public test methods are meant to be invoked from outside the test class, typically from other test classes. This is useful when you want to reuse a specific test scenario in multiple test classes.
  • Data Preparation for Other Test Methods: Public test methods can prepare test data and set up the environment for other test methods within the same test class. This is especially useful if several test methods require similar setup steps.

Private Test Methods

When to Use:

  • Helper Methods: Private test methods are typically used as helper methods within the same test class. They are not meant to be invoked from outside the test class but serve to break down the test logic into smaller, more manageable parts.
  • Avoiding External Invocation: If a method is specific to a particular test case and doesn’t need to be accessed from other test classes, it can be kept private to prevent accidental invocation and maintain encapsulation.

Test Data Isolation

Test classes should create their own test data within the test class to maintain isolation from the organization’s data. This ensures that tests run consistently and do not depend on the state of the organization’s data.

Use of System. Assert() and System.assertEquals()

Within test methods, you use assertions like System. assert() and System.assertEquals() to verify that the actual outcomes match the expected results. These assertions help you confirm that your code behaves correctly under different conditions.

Governor Limits in Test Class

Test classes are subject to the same governor limits as other Apex codes. It’s important to be aware of these limits and design your test methods to respect them. For instance, there are limits on the number of DML statements that your tests can consume.

Test.startTest() and Test.stopTest()

These methods are used to define the portion of code that is covered by a specific set of governor limits. These methods are particularly useful in unit tests, allowing us to separate the setup and execution phases of the test. Here’s what each method does:

Test.startTest()

The Test.startTest() method marks the beginning of the portion of code that is subject to a new set of governor limits. When you call Test.startTest(), Salesforce resets the governor limits for the current context. Any code executed after Test.startTest() is treated as a separate transaction, with its own set of limits. This allows you to test the bulk and governor limit aspects of your code in a controlled manner.

Use Cases:

  • Bulk Testing: If your code performs operations on a large number of records, you can use Test.startTest() to reset limits and simulate bulk processing.
  • Governor Limit Validation: To test that your code adheres to Salesforce’s governor limits, you can use this method to measure the exact usage within the test context.

Test.stopTest()

The Test.stopTest() method marks the end of the portion of code that is subject to the new set of governor limits established by Test.startTest(). When you call Test.stopTest(), Salesforce enforces the limits again, measuring the actual consumption of resources during the test. Any asynchronous processing triggered during the test context, such as batch jobs or future methods, will be executed immediately after Test.stopTest().

Use Case:

Validation of Asynchronous Code: If your code enqueues asynchronous jobs (e.g., batch classes, future methods), you can use Test.stopTest() to ensure that these jobs execute during the test context, allowing you to validate their behavior synchronously.

@testSetup Annotation

The @testSetup annotation allows you to create common test records that are available in all test methods within the test class. This improves test performance.

General Best Practices

  • Keep Test Methods Independent: Each test method should be independent and self-contained. Avoid relying on the order of execution or the outcomes of other test methods within the same test class.
  • Clear Naming Conventions: Use clear and descriptive names for both test classes and test methods. A well-named test method provides insight into its purpose, making the code more readable and understandable.
  • Regular Maintenance: Review and update test methods as the corresponding Apex code changes. Outdated or incorrect test methods can lead to false positives or negatives, making it essential to keep them aligned with the code they test.