Newer Version Available
IsTest アノテーション
isTest として定義されたクラスとメソッドは private または public のいずれかと宣言する必要があります。isTest として定義したクラスは最上位クラスである必要があります。
次に、2 つのテストメソッドを含む非公開テストクラスの例を示します。
1swfobject.registerObject("clippy.isTestExampleCode", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17@isTest
18private class MyTestClass {
19
20 // Methods for testing
21 @isTest static void test1() {
22 // Implement test code
23 }
24
25 @isTest static void test2() {
26 // Implement test code
27 }
28
29}1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17@isTest
18public class TestUtil {
19
20 public static void createTestAccounts() {
21 // Create some test accounts
22 }
23
24 public static void createTestContacts() {
25 // Create some test contacts
26 }
27
28}
29isTest として定義されたクラスは、インターフェースまたは enum 値とすることはできません。
公開テストクラスのメソッドは、実行中のテスト、つまり、テストメソッドまたはテストメソッドから呼び出されるコードからのみコールすることができ、テスト以外の要求からコールすることはできません。テストメソッドを実行できるさまざまな方法についての詳細は、「単体テストメソッドの実行」を参照してください。
IsTest(SeeAllData=true) アノテーション
Salesforce API バージョン 24.0 以降を使用して保存された Apex コードでは、テストクラスおよび個々のテストメソッドに対して、テストが作成していない既存のデータを含む組織のすべてのデータへのアクセス権を付与するには、isTest(SeeAllData=true) アノテーションを使用します。Salesforce API バージョン 24.0 以降を使用して保存した Apex コードでは、デフォルトで、テストメソッドは組織の既存のデータにアクセスできません。ただし、Salesforce API バージョン 23.0 以前で保存されたテストコードは、引き続き、組織のすべてのデータにアクセスでき、そのデータアクセス権は変わりません。「単体テストの組織データとテストデータの分離」を参照してください。
- IsTest(SeeAllData=true) アノテーションの考慮事項
-
- テストクラスが isTest(SeeAllData=true) アノテーションで定義されている場合、このアノテーションは、テストメソッドが @isTest アノテーションと testmethod キーワードのどちらを使用して定義されているかにかかわらず、すべてのテストメソッドに適用されます。
- isTest(SeeAllData=true) アノテーションは、クラスまたはメソッドレベルで適用される場合にデータにアクセスできるようにするために使用します。ただし、含まれているクラスがすでに isTest(SeeAllData=true) アノテーションで定義されている場合、メソッドでの isTest(SeeAllData=false) の使用によって、そのメソッドの組織データアクセスが制限されることはありません。この場合、メソッドは組織のすべてのデータにアクセスできます。
1swfobject.registerObject("clippy.SeeAllDataCode", "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}1swfobject.registerObject("clippy.SeeAllDataCode2", "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(OnInstall=true) アノテーション
IsTest(OnInstall=true) アノテーションを使用して、パッケージのインストール時に実行される Apex テストを指定します。このアノテーションは、管理パッケージまたは未管理パッケージのテストで使用されます。このアノテーションを付加したテストメソッドまたはこのアノテーションを含むテストクラスの一部であるメソッドのみが、パッケージのインストール時に実行されます。パッケージのインストールが成功するためには、パッケージのインストール時に実行というアノテーション指定されたテストが正常に終了する必要があります。パッケージのインストール時に失敗したテストをバイパスすることはできなくなりました。このアノテーションが付加されていないテストメソッドまたはクラス、または isTest(OnInstall=false) または isTest でアノテーションされたテストメソッドまたはクラスは、インストール時に実行されません。
次に、パッケージのインストール時に実行されるテストメソッドにアノテーションを付加する方法の例を示します。この例の test1 は実行されますが、test2 と test3 は実行されません。
1swfobject.registerObject("clippy.OnInstallCodeblock", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class OnInstallClass {
18 // Implement logic for the class.
19 public void method1(){
20 // Some code
21 }
22}
231swfobject.registerObject("clippy.OnInstallCodeblockTest", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17@isTest
18private class OnInstallClassTest {
19 // This test method will be executed
20 // during the installation of the package.
21 @isTest(OnInstall=true)
22 static void test1() {
23 // Some test code
24 }
25
26 // Tests excluded from running during the
27 // the installation of a package.
28
29 @isTest
30 static void test2() {
31 // Some test code
32 }
33
34 static testmethod void test3() {
35 // Some test code
36 }
37}
38