Newer Version Available
Using the isTest(SeeAllData=true) Annotation
Annotate your test class or test method with IsTest(SeeAllData=true) to open up
data access to records in your organization.
This 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.
1// All test methods in this class can access all data.
2@isTest(SeeAllData=true)
3public class TestDataAccessClass {
4
5 // This test accesses an existing account.
6 // It also creates and accesses a new test account.
7 static testmethod void myTestMethod1() {
8 // Query an existing account in the organization.
9 Account a = [SELECT Id, Name FROM Account WHERE Name='Acme' LIMIT 1];
10 System.assert(a != null);
11
12 // Create a test account based on the queried account.
13 Account testAccount = a.clone();
14 testAccount.Name = 'Acme Test';
15 insert testAccount;
16
17 // Query the test account that was inserted.
18 Account testAccount2 = [SELECT Id, Name FROM Account
19 WHERE Name='Acme Test' LIMIT 1];
20 System.assert(testAccount2 != null);
21 }
22
23
24 // Like the previous method, this test method can also access all data
25 // because the containing class is annotated with @isTest(SeeAllData=true).
26 @isTest static void myTestMethod2() {
27 // Can access all data in the organization.
28 }
29
30}This second example shows how to apply the @isTest(SeeAllData=true) annotation on a test
method. Because the test method’s class isn’t annotated, you have to annotate the method
to enable access to all data for the method. The second test method doesn’t have this
annotation, so it can access only the data it creates. In addition, it can access
objects that are used to manage your organization, such as
users.
1// This class contains test methods with different data access levels.
2@isTest
3private class ClassWithDifferentDataAccess {
4
5 // Test method that has access to all data.
6 @isTest(SeeAllData=true)
7 static void testWithAllDataAccess() {
8 // Can query all data in the organization.
9 }
10
11 // Test method that has access to only the data it creates
12 // and organization setup and metadata objects.
13 @isTest static void testWithOwnDataAccess() {
14 // This method can still access the User object.
15 // This query returns the first user object.
16 User u = [SELECT UserName,Email FROM User LIMIT 1];
17 System.debug('UserName: ' + u.UserName);
18 System.debug('Email: ' + u.Email);
19
20 // Can access the test account that is created here.
21 Account a = new Account(Name='Test Account');
22 insert a;
23 // Access the account that was just created.
24 Account insertedAcct = [SELECT Id,Name FROM Account
25 WHERE Name='Test Account'];
26 System.assert(insertedAcct != null);
27 }
28}- Considerations for the @IsTest(SeeAllData=true) Annotation
-
- If a test class is defined with the @isTest(SeeAllData=true) annotation, the annotation applies to all its test methods whether the test methods are defined with the @isTest annotation or the (deprecated) testMethod keyword.
- The @isTest(SeeAllData=true) annotation is used to open up data access when applied at the class or method level. However, if the containing class has been annotated with @isTest(SeeAllData=true), annotating a method with @isTest(SeeAllData=false) is ignored for that method. In this case, that method still has access to all the data in the organization. Annotating a method with @isTest(SeeAllData=true) overrides, for that method, an @isTest(SeeAllData=false) annotation on the class.
- @isTest(SeeAllData=true) and @isTest(isParallel=true) annotations cannot be used together on the same Apex method.