IsTest Annotation in Salesforce

IsTest Annotation in Salesforce

Last Updated on October 16, 2021 by Rakesh Gupta

Use the isTest annotation to define classes or individual methods that only contain code used for testing your application. The isTest annotation is similar to creating methods declared as test Method.

Note:– Classes defined with the isTest annotation don’t count against your organization limit of 3 MB for all Apex code.

Starting with Apex code saved using Salesforce.com API version 24.0, test methods don’t have access by default to pre-existing data in the organization. However, test code saved against Salesforce.com API version 23.0 or earlier continues to have access to all data in the organization and its data access is unchanged.

Classes and methods defined as isTest can be either private or public. Classes defined as isTest must be top-level classes.

This is an example of a private test class that contains two test methods.

@isTest
private class firstetstclass {

   // Methods for testing
   @isTest static void test1() {
      // Implement test code
   }

   @isTest static void test2() {
      // Implement test code
   }
}

This is an example of a public test class that contains a utility method for test data creation:
@isTest 
public class TestUtil {

   public static void createTestData() { 
      // Create some test invoices
   }

}

IsTest(SeeAllData=true)Annotation

For Apex code saved using Salesforce.com API version 24.0 and later, use the isTest(SeeAllData=true) annotation to grant test classes and individual test methods access to all data in the organization, including pre-existing data that the test didn’t create. Starting with Apex code saved using Salesforce.com API version 24.0, test methods don’t have access by default to pre-existing data in the organization. However, test code saved against Salesforce.com API version 23.0 or earlier continues to have access to all data in the organization and its data access is unchanged.

Considerations of the IsTest(SeeAllData=true) Annotation
  • If a test class is defined with the isTest(SeeAllData=true) annotation, this annotation applies to all its test methods whether the test methods are defined with the @isTest annotation or the test method keyword.
  • The isTest(SeeAllData=true) annotation is used to open up data access when applied at the class or method level. However, using isTest(SeeAllData=false) on a method doesn’t restrict organization data access for that method if the containing class has already been defined with the isTest(SeeAllData=true) annotation. In this case, the method will still have access to all the data in the organization.

 This is example shows how to define a test class with the isTest(SeeAllData=true) annotation. All the test methods in this class have access to all data in the organization.

// All test methods in this class can access all data.
@isTest(SeeAllData=true)
public class TestDataAccessClass {

    // This test accesses an existing merchandise item. 
    // It also creates and accesses a new test merchandise item.
    static testmethod void myTestMethod1() {
        // Query an existing merchandise item in the organization. 
        Merchandise__c m = [SELECT Id, Price__c, Total_Inventory__c, Description__c 
                            FROM Merchandise__c WHERE Name='Pencils' LIMIT 1];
        System.assert(m != null);

        // Create a test merchandise item based on the queried merchandise item.
        Merchandise__c testMerchandise = m.clone();
        testMerchandise.Name = 'Test Pencil';
        insert testMerchandise;

        // Query the test merchandise that was inserted.
        Merchandise__c testMerchandise2 = [SELECT Id, Price__c, Total_Inventory__c 
                            FROM Merchandise__c WHERE Name='Test Pencil' LIMIT 1];
        System.assert(testMerchandise2 != null);
    }

    // Like the previous method, this test method can also access all data
    // because the containing class is annotated with @isTest(SeeAllData=true).
    @isTest static void myTestMethod2() {
        // Can access all data in the organization.
    }

}

This second example shows how to apply theisTest(SeeAllData=true)annotation on a test method. Because the class that the test method is contained in isn’t defined with this annotation, you have to apply this annotation on the test method to enable access to all data for that test method. The second test method doesn’t have this annotation, so it can access only the data it creates in addition to objects that are used to manage your organization, such as users.

// This class contains test methods with different data access levels.
@isTest
private class ClassWithDifferentDataAccess {

    // Test method that has access to all data.
    @isTest(SeeAllData=true)
    static void testWithAllDataAccess() {
        // Can query all data in the organization.      
    }

    // Test method that has access to only the data it creates
    // and organization setup and metadata objects.
    @isTest static void testWithOwnDataAccess() {
        // This method can still access the User object.
        // This query returns the first user object.
        User u = [SELECT UserName,Email FROM User LIMIT 1]; 
        System.debug('UserName: ' + u.UserName);
        System.debug('Email: ' + u.Email);

        // Can access the test invoice that is created here.
        Invoice_Statement__c inv = new Invoice_Statement__c(
                                   Description__c='Invoice 1');
        insert inv;      
        // Access the invoice that was just created.
        Invoice_Statement__c insertedInv = [SELECT Id,Description__C 
                                FROM Invoice_Statement__c
                                WHERE Description__c='Invoice 1'];
        System.assert(insertedInv != null);
    }
} 

If you have feedback, suggestions for posts or need more information about Salesforce online training program running by me. Say hello and leave and message to me!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.