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

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

IsTest(SeeAllData=true) を使用して、テストクラスまたはテストメソッドにアノテーションを付加して、組織のレコードへのデータアクセス権を開放します。
この例では、isTest(SeeAllData=true) アノテーションを使用してテストクラスを定義する方法を示します。このクラスのすべてのテストメソッドは組織のすべてのデータにアクセスできます。
1swfobject.registerObject("clippy.d76130e37-d76144e183", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// All test methods in this class can access all data.
18@isTest(SeeAllData=true)
19public class TestDataAccessClass {
20
21    // This test accesses an existing account. 
22    // It also creates and accesses a new test account.
23    static testmethod void myTestMethod1() {
24        // Query an existing account in the organization. 
25        Account a = [SELECT Id, Name FROM Account WHERE Name='Acme' LIMIT 1];
26        System.assert(a != null);
27        
28        // Create a test account based on the queried account.
29        Account testAccount = a.clone();
30        testAccount.Name = 'Acme Test';
31        insert testAccount;
32        
33        // Query the test account that was inserted.
34        Account testAccount2 = [SELECT Id, Name FROM Account 
35                                WHERE Name='Acme Test' LIMIT 1];
36        System.assert(testAccount2 != null);
37    }
38       
39    
40    // Like the previous method, this test method can also access all data
41    // because the containing class is annotated with @isTest(SeeAllData=true).
42    @isTest static void myTestMethod2() {
43        // Can access all data in the organization.
44   }
45  
46}
この 2 番目の例では、テストメソッドに isTest(SeeAllData=true) アノテーションを適用する方法を示します。このクラスはテストメソッドに含まれているけれども、このアノテーションを使用して定義されていないため、テストメソッドがすべてのデータにアクセスできるようにするには、このアノテーションをテストメソッドに適用する必要があります。2 番目のテストメソッドにはこのアノテーションはありません。そのため、テストメソッドでアクセスできるデータはテストメソッドで作成されたデータに加え、組織の管理に使用するオブジェクトのデータになります。組織の管理に使用するオブジェクトの例としてユーザがあげられます。
1swfobject.registerObject("clippy.d76130e39-d76144e191", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// This class contains test methods with different data access levels.
18@isTest
19private class ClassWithDifferentDataAccess {
20
21    // Test method that has access to all data.
22    @isTest(SeeAllData=true)
23    static void testWithAllDataAccess() {
24        // Can query all data in the organization.      
25    }
26    
27    // Test method that has access to only the data it creates
28    // and organization setup and metadata objects.
29    @isTest static void testWithOwnDataAccess() {
30        // This method can still access the User object.
31        // This query returns the first user object.
32        User u = [SELECT UserName,Email FROM User LIMIT 1]; 
33        System.debug('UserName: ' + u.UserName);
34        System.debug('Email: ' + u.Email);
35        
36        // Can access the test account that is created here.
37        Account a = new Account(Name='Test Account');
38        insert a;      
39        // Access the account that was just created.
40        Account insertedAcct = [SELECT Id,Name FROM Account 
41                                WHERE Name='Test Account'];
42        System.assert(insertedAcct != null);
43    }
44}
IsTest(SeeAllData=true) アノテーションの考慮事項
  • テストクラスが isTest(SeeAllData=true) アノテーションで定義されている場合、このアノテーションは、テストメソッドが @isTest アノテーションと testmethod キーワードのどちらを使用して定義されているかにかかわらず、すべてのテストメソッドに適用されます。
  • isTest(SeeAllData=true) アノテーションは、クラスまたはメソッドレベルで適用される場合にデータにアクセスできるようにするために使用します。ただし、含まれているクラスがすでに isTest(SeeAllData=true) アノテーションで定義されている場合、メソッドでの isTest(SeeAllData=false) の使用によって、そのメソッドの組織データアクセスが制限されることはありません。この場合、メソッドは組織のすべてのデータにアクセスできます。