• #30913
     Prachi #9
    Forcetalks

    What is @testSetup annotation? When you will use it?

    What is @testSetup annotation? When you will use it?

     

    #30953
    Forcetalks

    Hello Prachi,

    For example, we have a test class, It has two test methods and each test method required 5 account records. So without creating 5 records for each test method, we will create a new method with annotation @testSetup then create only 5 account records. These records can access all test methods using SOQL query in that test class.
    Ex: @testSetup method
    @testSetup static void setup() {
    // Create common test accounts
    List<Account> accountsLst = new List<Account>();
    for(Integer i=0;i<5;i++) {
    testAccts.add(new Account(Name = ‘TestAc ‘+i));
    }
    Insert accountsLst;
    }
    Thinks to remember when you use @testSetup method:

    1. We can use only one test setup method per class.
    2. Test setup method will execute first in the test class.
    3. If any DML error occurs in this test setup method, the rest of the method not executed.
    4. If a test setup method calls a non-test method of another class, no code coverage is calculated for the non-test method.

    Thanks.

    #34551
    Salesforce | Parul Parul #2
    Forcetalks

    If a test class contains a test setup method, the testing framework executes the test setup method first, before any test method in the class.

    Records that are created in a test setup method are available to all test methods in the test class and are rolled back at the end of test class execution.

    If a test method changes those records, such as record field updates or record deletions, those changes are rolled back after each test method finishes execution. The next executing test method gets access to the original unmodified state of those records.

    Syntax;

    @testSetup static void methodName() {

    }

    Ex:

    @iSTest
    public class LeadProcessorTest {

    //below test setup method will be invoked
    // implicitly before every test methods
    @testsetup
    static void createLead(){
    List<Lead> lstLead = new List<Lead>();
    for(Integer i = 0 ; i<200 ; i++) {
    lstLead.add(new Lead(lastName = ‘testLName’+i , Company = ‘Salesforce’));
    }
    insert lstLead ;
    }

    //Check whether records created in test method
    //is accessible or not
    public static testMethod void test(){
    System.assertEquals(200, [SELECT COUNT() FROM Lead Where company = ‘Salesforce’]);
    }
    }

     

    Thanks

    #36132
     Anjali #8
    Forcetalks

    Hi Prachi,

    All methods that are annotated with @testSetup will be called test setup methods.

    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.
Viewing 4 posts - 1 through 4 (of 4 total)

Please to reply to this topic.

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2018 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?