DEV Community

Cover image for Salesforce: Leveling Up with Apex Testing
Priyank Sevak
Priyank Sevak

Posted on • Edited on

Salesforce: Leveling Up with Apex Testing

Think of your code as a house. Testing is like a home inspector – it reveals hidden flaws before they become major problems.

Just like a home inspector ensures your dream home is structurally sound, testing your code is crucial for guaranteeing its reliability and functionality. In the world of Salesforce development, Apex testing plays this vital role. It's a structured approach to simulating real-world scenarios and verifying if your code behaves as intended. Here's how Apex testing empowers you as a developer:

Benefits of Apex Testing:

  • Uncover Hidden Defects: Imagine deploying code riddled with production errors! Testing acts as a safety net, identifying and eliminating potential bugs before they disrupt your users' experience.

  • Confidence in Code Changes: Ever hesitate to modify existing code for fear of breaking something? Robust testing provides peace of mind, allowing you to confidently enhance functionalities without introducing regressions.

  • Meeting Deployment Requirements: Salesforce enforces a minimum code coverage of 75% for deployment. Effective Apex testing ensures your code meets this threshold, enabling smooth deployments.

Image description

The Solution - Diving into Apex Testing:

@isTest
private class MyTestClass{
  @isTest
  static void myTest(){
     //Your Test codes
  }

}
Enter fullscreen mode Exit fullscreen mode

Sandbox Testing: A Clean Slate

In Salesforce, Apex test classes operate in a sandbox environment, isolated from your production data. This means you can create and manipulate test data without affecting real-world records.

Understanding Data Access:

While test classes can't directly access your existing data, they can access setup and metadata objects like Users and Profiles. This provides a controlled environment for testing without risking production data.

Leveraging Test Data:

Creating test data is essential for effective testing. You can generate it within your test classes or use utility classes for reusable data creation. This allows you to simulate various scenarios and ensure your code functions as expected.

Benefits of Sandbox Testing:

  • Isolated Environment: Prevents accidental changes to production data.
  • Controlled Testing: Enables focused testing without external factors.
  • Data Flexibility: Create and modify test data to simulate different scenarios.

Utilizing @testSetup:

@isTest
private class CommonTestSetup {

    @testSetup static void setup() {
        // Create common test accounts
        List<Account> testAccts = new List<Account>();
        for(Integer i=0;i<2;i++) {
            testAccts.add(new Account(Name = 'TestAcct'+i));
        }
        insert testAccts;        
    }

    @isTest static void testMethod1() {
        // Get the first test account by using a SOQL query
        Account acct = [SELECT Id FROM Account WHERE Name='TestAcct0' LIMIT 1];
        // Modify first account
        acct.Phone = '555-1212';
        // This update is local to this test method only.
        update acct;

        // Delete second account
        Account acct2 = [SELECT Id FROM Account WHERE Name='TestAcct1' LIMIT 1];
        // This deletion is local to this test method only.
        delete acct2;

        // Perform some testing
    }
}
Enter fullscreen mode Exit fullscreen mode

Things to remember while using @testSetup:

  • Each test class can only have one @testSetup method.
  • The entire test class fails if this method encounters a critical error, like a failed DML operation or assertion.
  • Additionally, if the @testSetup method calls a non-test method, code coverage won't be calculated for that non-test method.

Buy Me A Coffee

Top comments (0)