IsTest アノテーション
@isTest として定義されたクラスとメソッドは private または public のいずれかと宣言する必要があります。@isTest として定義されたクラスは最上位クラスである必要があります。
次に、2 つのテストメソッドを含む非公開テストクラスの例を示します。
1@isTest
2private class MyTestClass {
3
4 // Methods for testing
5 @isTest static void test1() {
6 // Implement test code
7 }
8
9 @isTest static void test2() {
10 // Implement test code
11 }
12
13}1@isTest
2public class TestUtil {
3
4 public static void createTestAccounts() {
5 // Create some test accounts
6 }
7
8 public static void createTestContacts() {
9 // Create some test contacts
10 }
11
12}@isTest として定義されたクラスは、インターフェースまたは列挙値とすることはできません。
公開テストクラスのメソッドは、実行中のテスト、つまり、テストメソッドまたはテストメソッドから呼び出されるコードからのみコールすることができます。公開メソッドは、テスト以外の要求からコールすることはできません。テストメソッドを実行できるさまざまな方法についての詳細は、「単体テストメソッドの実行」を参照してください。
@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) アノテーションの付加は無視されます。この場合、そのメソッドは組織のすべてのデータにアクセスできます。メソッドへの @isTest(SeeAllData=true) アノテーションの付加は、そのメソッドにおいて、クラスの @isTest(SeeAllData=false) アノテーションよりも優先されます。
- @isTest(SeeAllData=true) アノテーションと @isTest(isParallel=true) アノテーションは、同じ Apex メソッドで同時に使用することはできません。
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}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(OnInstall=true) アノテーション
@IsTest(OnInstall=true) アノテーションを使用して、パッケージのインストール時に実行される Apex テストを指定します。このアノテーションは、管理パッケージまたは未管理パッケージのテストで使用されます。このアノテーションを付加したテストメソッドまたはこのアノテーションを含むテストクラスの一部であるメソッドのみが、パッケージのインストール時に実行されます。パッケージのインストールが成功するためには、パッケージのインストール時に実行というアノテーション指定されたテストが正常に終了する必要があります。パッケージのインストール時に失敗したテストをバイパスすることはできなくなりました。このアノテーションが付加されていないテストメソッドまたはクラス、あるいは @isTest(OnInstall=false) または @isTest でアノテーションされたテストメソッドまたはクラスは、インストール時に実行されません。
次に、パッケージのインストール時に実行されるテストメソッドにアノテーションを付加する方法の例を示します。この例の test1 は実行されますが、test2 と test3 は実行されません。
1public class OnInstallClass {
2 // Implement logic for the class.
3 public void method1(){
4 // Some code
5 }
6}1@isTest
2private class OnInstallClassTest {
3 // This test method will be executed
4 // during the installation of the package.
5 @isTest(OnInstall=true)
6 static void test1() {
7 // Some test code
8 }
9
10 // Tests excluded from running during the
11 // the installation of a package.
12
13 @isTest
14 static void test2() {
15 // Some test code
16 }
17
18 static testmethod void test3() {
19 // Some test code
20 }
21}