Tuesday 23 June 2015

Spring 15 Test Setup(@testSetup) Method in Salesforce

Spring’15 release came up with @testSetup method. This is one of the good features in this release for Test Class. Let’s discuss the benefits, usage, and limitations of this new feature.
Benefits
  1. By setting up records once for the class, you don’t need to re-create records for each test method. It’s a good time saver.
  2. Reduces test execution times, especially when you’re working with many records.
Usage of Test Setup Method
         In previous releases, while writing test classes, if you need same set of data in different methods, we created a common utility class with test records and call the utility method wherever we need it. So, calling the utility method multiple class from a Test class takes a longer time to finish the execution.

       The new Test Setup method speeds up the Test Execution. Let us see an example as to how to use the Test Setup method,

  1. @isTest
  2. private class TestSetupClass
  3. {
  4.     @testSetup static void insertContact()
  5.     {
  6.         Contact cnt = new Contact();
  7.         cnt.LastName = 'Arunkumar';
  8.         cnt.Email = 'testcontact@test.com';
  9.         insert cnt;
  10.     }
  11.    
  12.     static testMethod void updateContactEmail()
  13.     {
  14.         Contact cnt = [SELECT Name, Email FROM Contact];
  15.         System.assertEquals(cnt.Email'testcontact@test.com');
  16.        
  17.         // update contact email address. This update is local to this test method only.
  18.         cnt.Email = 'testupdated@test.com';
  19.         update cnt;
  20.        
  21.         System.assertEquals(cnt.Email'testupdated@test.com');
  22.         // Salesforce automatically rolled back to the original value.
  23.     }
  24.    
  25.     static testMethod void verifyTheOriginalEmail()
  26.     {
  27.         Contact cnt = [SELECT Name, Email FROM Contact];
  28.         // verify the email original email address.
  29.         System.assertEquals(cnt.Email'testcontact@test.com');
  30.     }
  31. }
  1. In the above test class, we inserted the Contact record in the Test Setup Method. So the inserted contact record is accessible in all test methods within the test class.
  2. Test setup methods are defined in a test class, take no arguments, and return no value.
  3. If a test class contains a test setup method, the testing framework executes the test setup method first, before any test method is executed in the class.

Limitation
As per salesforce documentation, take a look on the below limitations before using this method,
  1. Test setup methods aren’t supported for @isTest (SeeAllData=true) annotation test classes. Hence, you can use this feature from the API version 24.0 or above.
  2. Multiple test setup methods are allowed in a test class, but the order in which they’re executed by the testing framework isn’t guaranteed.
  3. If a fatal error occurs during the execution of a test setup method, such as an exception that’s caused by a DML operation or an assertion failure, the entire test class fails, and no further tests in the class are 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.

Reference

No comments:

Post a Comment

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...