@testSetup annotation in Apex

In Salesforce, the @testSetup annotation is used in Apex tests to create test data that can be reused across multiple test methods within the same test class. This annotation is especially useful for making common test data required for various test methods, reducing redundancy, and improving test efficiency.

Here's an example of how you can use the @testSetup method in Salesforce:

@isTest
private class MyTestClass {
    
    // Define test data setup method
    @testSetup
    static void setupTestData() {
        // Create test records
        Account acc = new Account(Name='Test Account');
        insert acc;
        
        Contact con = new Contact(LastName='Test Contact', AccountId=acc.Id);
        insert con;
        
        // You can create more test records as needed
    }
    
    // Test method 1
    @isTest
    static void testMethod1() {
        // Test logic using the test data created in setupTestData method
        // For example:
        Account testAcc = [SELECT Id, Name FROM Account WHERE Name='Test Account' LIMIT 1];
        System.assertEquals('Test Account', testAcc.Name);
    }
    
    // Test method 2
    @isTest
    static void testMethod2() {
        // Test logic using the test data created in setupTestData method
        // For example:
        List<Contact> testContacts = [SELECT Id, LastName FROM Contact WHERE LastName='Test Contact' LIMIT 1];
        System.assertEquals(1, testContacts.size());
        System.assertEquals('Test Contact', testContacts[0].LastName);
    }

}

In this example:

The @testSetup method setupTestData() is used to create test records for the Account and Contact objects.

The @isTest annotation is applied to the test class and each test method.

The testMethod1() and testMethod2() methods demonstrate how to access and utilize the test data created in the setupTestData() method within test methods.

Using @testSetup, you can ensure that the test data setup is executed only once before all test methods in the test class, optimizing test performance and avoiding redundancy in data creation.

Test Setup Method Considerations

Test setup methods are supported only with a test class's default data isolation mode. If the test class or a test method has access to organization data using the @isTest(SeeAllData=true) annotation, test setup methods aren't supported in this class. Because data isolation for tests is available for API versions 24.0 and later, test setup methods are also available for those versions only.

You can have only one test setup method per test class.

If a fatal error occurs during the execution of a test setup method, such as an exception caused by a DML operation or an assertion failure, the entire test class fails, and no further tests are executed.

If a test setup method calls a non-test method of another class, no code coverage is calculated for the non-test method.


Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach