TestSetUp method in apex test classes

TestSetUp method in apex test classes

@testsetup annotation in test class method

Test classes are an important part of overall SDLC in Salesforce. We as a developer have to write the test classes very often, and we need to create the test data as well, to have our test class executed successfully. We’ll cover a new concept introduced in salesforce test classes i.e TestSetUp method in apex test classes.

Initially, we had to create test data in every test method, because every test method is considered a different transaction with its own governor limits. But Salesforce has introduced @TestSetUp annotation for test class method. You can write a method in test class, with @TestSetUp annotation applied, and create all your common test data in this method.

Here are few key points about @TestSetUp methods:

  • Method marked with @TestSetUp annotation executes before any testMethod.
  • Data created in this method doesn’t need to be created again and again, and it is by default available for all test methods.
  • There can be only one setup method per test class.
  • Test setup methods are supported only with the default data isolation mode for a test class. If the test class or a test method has access to organization data by using the @isTest(SeeAllData=true) annotation, test setup methods aren’t supported in this class. 
  • Test setup methods are available for 24.0 or later versions only.
  • Every test method will get unchanged version of the test data created in setup method, doesn’t matter if any other test method has modified the data. We will show this in testMethod2 of below example.

 

@testsetup Example

Below is a sample code, which will show how the test data is available in every test method.


@isTest
private class TestSetupMethodExample {
    //Below is a method with @testsetup annotation, the name can be anything like setup(), oneTimeData(), etc.
    @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() {
        // Here, we will see if test data created in setup method is available or not, 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
    }

    @isTest static void testMethod2() {
        // The changes made by testMethod1() are rolled back and
        // are not visible to this test method.
        // Get the first account by using a SOQL query
        Account acct = [SELECT Phone FROM Account WHERE Name='TestAcct0' LIMIT 1];
        // Verify that test account created by test setup method is unaltered.
        System.assertEquals(null, acct.Phone);

        // Get the second account by using a SOQL query
        Account acct2 = [SELECT Id FROM Account WHERE Name='TestAcct1' LIMIT 1];
        // Verify test account created by test setup method is unaltered.
        System.assertNotEquals(null, acct2);

        // Perform some testing
    }

}

Because, test data created is less in number, that’s why rolling back of records at the end of test class takes less time. This actually ends up in improving the performance and reducing the time take to run the test class.

For more details refer this official link @TestSetup Annotation

For more details about test class test class in salesforce

Best of luck with your test class and test data !! Happy coding 🙂

Permanent link to this article: https://www.sfdcpoint.com/salesforce/testsetup-method-in-apex-test-classes/

2 comments

    • aman on June 7, 2020 at 12:32 pm
    • Reply

    amazing info

    • Nikhil on August 10, 2020 at 8:32 pm
    • Reply

    great information.It is really helpful.
    Thanks.

Leave a Reply

Your email address will not be published.