この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

isTest(SeeAllData=true) アノテーションの使用

IsTest(SeeAllData=true) を使用して、テストクラスまたはテストメソッドにアノテーションを付加して、組織のレコードへのデータアクセス権を開放します。
この例では、@isTest(SeeAllData=true) アノテーションを使用してテストクラスを定義する方法を示します。このクラスのすべてのテストメソッドは組織のすべてのデータにアクセスできます。
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}
この 2 番目の例では、テストメソッドに @isTest(SeeAllData=true) アノテーションを適用する方法を示します。テストメソッドのクラスにはアノテーションが付加されていないため、メソッドがすべてのデータにアクセスできるようにするには、メソッドにアノテーションを付加する必要があります。2 番目のテストメソッドにはこのアノテーションはありません。そのため、テストメソッドでアクセスできるデータはテストメソッドで作成されたデータのみになります。組織の管理に使用するオブジェクト (ユーザなど) にはアクセスできます。
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}
@IsTest(SeeAllData=true) アノテーションの考慮事項
  • テストクラスが @isTest(SeeAllData=true) アノテーションで定義されている場合、このアノテーションは、テストメソッドが @isTest アノテーションと (非推奨の) testMethod キーワードのどちらを使用して定義されているかにかかわらず、すべてのテストメソッドに適用されます。
  • @isTest(SeeAllData=true) アノテーションは、クラスまたはメソッドレベルで適用される場合にデータにアクセスできるようにするために使用します。ただし、含まれているクラスにすでに @isTest(SeeAllData=true) アノテーションが付加されている場合、メソッドへの @isTest(SeeAllData=false) アノテーションの付加は無視されます。この場合、そのメソッドは組織のすべてのデータにアクセスできます。メソッドへの @isTest(SeeAllData=true) アノテーションの付加は、そのメソッドにおいて、クラスの @isTest(SeeAllData=false) アノテーションよりも優先されます。
  • @isTest(SeeAllData=true) アノテーションと @isTest(isParallel=true) アノテーションは、同じ Apex メソッドで同時に使用することはできません。