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

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

IsTest(SeeAllData=true) を使用して、テストクラスまたはテストメソッドにアノテーションを付加して、組織のレコードへのデータアクセス権を開放します。IsTest(SeeAllData=true) アノテーションはデータク��リに適用されますが、レコードの作成または変更 (削除を含む) には適用されません。アノテーションを使用する場合でも、新規レコードおよび変更されたレコードは Apex テストでロールバックされます。

クラスに @isTest(SeeAllData=true) アノテーションを付加することによって、テストメソッドからすべての組織レコードにアクセスできるようになります。ただし、最善の方法は、データサイロで @isTest(SeeAllData=false) を使用して Apex テストを実行することです。使用している API のバージョンによっては、デフォルトのアノテーションが異なる場合があります。

警告

この例では、@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    @IsTest
8    static void myTestMethod1() {
9        // Query an existing account in the organization. 
10        Account a = [SELECT Id, Name FROM Account WHERE Name='Acme' LIMIT 1];
11        System.assert(a != null);
12        
13        // Create a test account based on the queried account.
14        Account testAccount = a.clone();
15        testAccount.Name = 'Acme Test';
16        insert testAccount;
17        
18        // Query the test account that was inserted.
19        Account testAccount2 = [SELECT Id, Name FROM Account 
20                                WHERE Name='Acme Test' LIMIT 1];
21        System.assert(testAccount2 != null);
22    }
23       
24    
25    // Like the previous method, this test method can also access all data
26    // because the containing class is annotated with @IsTest(SeeAllData=true).
27    @IsTest
28    static void myTestMethod2() {
29        // Can access all data in the organization.
30   }
31  
32}
この 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
14    static void testWithOwnDataAccess() {
15        // This method can still access the User object.
16        // This query returns the first user object.
17        User u = [SELECT UserName,Email FROM User LIMIT 1]; 
18        System.debug('UserName: ' + u.UserName);
19        System.debug('Email: ' + u.Email);
20        
21        // Can access the test account that is created here.
22        Account a = new Account(Name='Test Account');
23        insert a;      
24        // Access the account that was just created.
25        Account insertedAcct = [SELECT Id,Name FROM Account 
26                                WHERE Name='Test Account'];
27        System.assert(insertedAcct != null);
28    }
29}
@IsTest(SeeAllData=true) アノテーションの考慮事項
  • テストクラスが @IsTest(SeeAllData=true) アノテーションで定義されている場合、SeeAllData=true は、SeeAllData キーワードを明示的に設定していないすべてのテストメソッドに適用されます。
  • @IsTest(SeeAllData=true) アノテーションは、クラスまたはメソッドレベルで適用される場合にデータにアクセスできるようにするために使用します。ただし、含まれているクラスにすでに @IsTest(SeeAllData=true) アノテーションが付加されている場合、メソッドへの @IsTest(SeeAllData=false) アノテーションの付加は無視されます。この場合、そのメソッドは組織のすべてのデータにアクセスできます。メソッドへの @IsTest(SeeAllData=true) アノテーションの付加は、そのメソッドにおいて、クラスの @IsTest(SeeAllData=false) アノテーションよりも優先されます。
  • @IsTest(SeeAllData=true) アノテーションと @IsTest(IsParallel=true) アノテーションは、同じ Apex メソッドで同時に使用することはできません。