Wednesday, 27 May 2015

Using the isTest(seeAllData = true) Annotation



If we set the annotation @isTest(seeAllData = true) in the test class then that class will have access to all data in the organization.

Example Class:

  1. @isTest(seeAllData = true)
  2. Public Class Test_AccountClass
  3. {
  4.                 Static testmethod void testAccountMethod1()
  5.                 {
  6.                                 Account ac = [select Name from account Limit 1];
  7.                                 System.assert(ac != null);
  8.                 }
  9.                 Static testmethod void testAccountMethod2()
  10.                 {
  11.                                 // This method will also have access to all org data.
  12.                 }
  13. }
If we are using only @isTest, then it will not have access to all org data, but it will have access to the object that are used to manage our organization.

Object used for manage Organization:

  • User
  • Profile
  • Organization
  • AsyncApexJob
  • CornTrigger
  • RecordType
  • Apex Class
  • Apex trigger
  • Apex Component
  • Apex Page

Example Class:

  1. @isTest
  2. Public Class Test_AccountClass
  3. {
  4.                 @isTest(seeAlldata = true)
  5. Static testmethod void testAccountMethod1()
  6.                 {
  7.                                 // this method will have access all the org data.
  8.                                 Account ac = [select Name from account Limit 1];
  9.                                 System.assert(ac != null);
  10.                 }
  11.                 @isTest Static testmethod void testAccountMethod2()
  12.                 {
  13.                                 // This method will have access only to the above mentioned object.eg:User
  14.                                 User u =[select Id,Name from User Limit 1];
  15.                                 System.assert(u != null);
  16.                 }
  17. }
This annotation will work for the test class saved using the salesforce API version 24.0 or latter calls. For earlier version i.e. 23.0 or earlier continues to have access to all data in the organization and its data access is unchanged.

No comments:

Post a Comment