• +91 9311 5511 17
  • info@websharpmarketing.com
Web Sharp Marketing Web Sharp Marketing
Get A Quote
  • Home
  • About Us
  • Our Services
    • Full Stack Development
    • Salesforce Training
    • Salesforce Solutions
    • Data Migration and Processing
    • Code Review, QA and Support
    • Website Designing
  • Blog
  • Contact Us
  • February 24, 2024
  • WSM
  • 0 Comments
  • 377 Views
  • 0 Likes
  • Apex

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
@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

Thanks for reading this article, we will provide you more articles related to Apex salesforce. Good Luck 🙂

Tags:
@testsetupApexSalesforceTest Classunit Test
Prev PostWhat is Test class in Salesforce Apex
Next PostSending Automated Emails to All Related Contact Roles When an Opportunity is Won
Related Posts
  • Lightning Component(Aura) To Display Contacts Related To Particular Account August 22, 2019

Leave a Comment Cancel Comment

Your email address will not be published. Required fields are marked *

WSM Technosoft’s offering enables clients to quickly augment their Salesforce Consultant teams through product development and support resources located in our development centers. Based upon project’s requirements, WSM Technosoft’s Team can either co-locate at client at site or work from our development center in India.

Our Services

  • Website Designing
  • Salesforce Solutions
  • Salesforce Training
  • Data Migration and Processing
  • Code Review, QA and Support

Recent Articles

  • How to get your work history report

    Aug 21, 2024

  • How Salesforce Delivers Exceptional

    Mar 20, 2024

Useful Links

  • Home
  • Portfolio
  • Contacts
  • Services
  • FAQ
  • About Us
  • Services
  • Blog
  • About Us
  • Contacts
info@websharpmarketing.com
+91 9311 5511 17 Call Us Now
2nd Floor, Devasthali - Plot no. 42/5, Block – A, Sector – 62, Noida, UP - 281203
© WSM Technosoft, 2014–2024. All rights reserved