No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
テストクラス
名前空間
Test メソッド
Test のメソッドは次のとおりです。すべてのメソッドが静的です。
getStandardPricebookId()
署名
public static Id getStandardPricebookId()
使用方法
このメソッドでは、テストで組織データをクエリできるかどうかに関係なく、組織内の標準価格表の ID を返します。デフォルトでは、@isTest(SeeAllData=true) アノテーションが付加されていない限り、テストで組織データをクエリすることはできません。
標準価格で価格表エントリを作成するには、標準価格表の ID が必要です。このメソッドを使用して標準価格表 ID を取得すれば、テストで価格表エントリを作成できます。
例
次の例では、価格表エントリのテストデータをいくつか作成します。この例のテストメソッドは、標準価格表 ID を取得し、その ID を使用して標準価格で商品の価格表エントリを作成します。次に、テストはカスタム価格表を作成し、このカスタム価格表の ID を使用して、カスタム価格の価格表エントリを追加します。
1swfobject.registerObject("clippy.codeblock-0", "9");@isTest
2public class PriceBookTest {
3 // Utility method that can be called by Apex tests to create price book entries.
4 static testmethod void addPricebookEntries() {
5 // First, set up test price book entries.
6 // Insert a test product.
7 Product2 prod = new Product2(Name = 'Laptop X200',
8 Family = 'Hardware');
9 insert prod;
10
11 // Get standard price book ID.
12 // This is available irrespective of the state of SeeAllData.
13 Id pricebookId = Test.getStandardPricebookId();
14
15 // 1. Insert a price book entry for the standard price book.
16 // Standard price book entries require the standard price book ID we got earlier.
17 PricebookEntry standardPrice = new PricebookEntry(
18 Pricebook2Id = pricebookId, Product2Id = prod.Id,
19 UnitPrice = 10000, IsActive = true);
20 insert standardPrice;
21
22 // Create a custom price book
23 Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
24 insert customPB;
25
26 // 2. Insert a price book entry with a custom price.
27 PricebookEntry customPrice = new PricebookEntry(
28 Pricebook2Id = customPB.Id, Product2Id = prod.Id,
29 UnitPrice = 12000, IsActive = true);
30 insert customPrice;
31
32 // Next, perform some tests with your test price book entries.
33 }
34}isRunningTest()
署名
public static Boolean isRunningTest()
戻り値
型: Boolean
loadData(Schema.SObjectType, String)
署名
public static List<sObject> loadData(Schema.SObjectType sObjectToken, String resourceName)
パラメータ
- sObjectToken
- 型: Schema.SObjectType
- テストレコードを挿入する sObject 型。
- resourceName
- 型: String
- 読み込むテストレコードを含む .csv ファイルに対応する静的リソース。この名前は大文字と小文字を区別しません。
setCurrentPage(PageReference)
署名
public static Void setCurrentPage(PageReference page)
パラメータ
- page
- 型: System.PageReference
戻り値
型: Void
setCurrentPageReference(PageReference)
署名
public static Void setCurrentPageReference(PageReference page)
パラメータ
- page
- 型: System.PageReference
戻り値
型: Void
setFixedSearchResults(ID[])
署名
public static Void setFixedSearchResults(ID[] opt_set_search_results)
パラメータ
- opt_set_search_results
- 型: ID[]
- opt_set_search_results で指定されたレコード ID のリストは、WHERE 句または LIMIT 句に指定されていない場合、通常は SOSL クエリで返される結果を置き換えます。これらの句が SOSL クエリにある場合、固定された検索結果のリストに適用されます。
戻り値
型: Void
使用方法
opt_set_search_results が指定されていない場合、後続のすべての SOSL クエリは結果を返しません。
詳細は、「SOSL クエリの単体テストへの追加」を参照してください。
setMock(Type, Object)
署名
public static Void setMock(Type interfaceType, Object instance)
パラメータ
- interfaceType
- 型: System.Type
- instance
- 型: Object
戻り値
型: Void
使用方法
setReadOnlyApplicationMode(Boolean)
署名
public static Void setReadOnlyApplicationMode(Boolean application_mode)
パラメータ
- application_mode
- 型: Boolean
戻り値
型: Void
使用方法
setReadOnlyApplicationMode は、5 分アップグレードの一部として提供されます。「getApplicationReadWriteMode()」 System メソッドも参照してください。
例
次の例では、アプリケーションモードを参照のみに設定し、新しい取引先レコードを挿入しようとしています。結果は例外となります。その後、アプリケーションモードはリセットされ、正しく挿入されます。
1@isTest
2private class ApplicationReadOnlyModeTestClass {
3 public static testmethod void test() {
4 // Create a test account that is used for querying later.
5 Account testAccount = new Account(Name = 'TestAccount');
6 insert testAccount;
7
8 // Set the application read only mode.
9 Test.setReadOnlyApplicationMode(true);
10
11 // Verify that the application is in read-only mode.
12 System.assertEquals(
13 ApplicationReadWriteMode.READ_ONLY,
14 System.getApplicationReadWriteMode());
15
16 // Create a new account object.
17 Account testAccount2 = new Account(Name = 'TestAccount2');
18
19 try {
20 // Get the test account created earlier. Should be successful.
21 Account testAccountFromDb =
22 [SELECT Id, Name FROM Account WHERE Name = 'TestAccount'];
23 System.assertEquals(testAccount.Id, testAccountFromDb.Id);
24
25 // Inserts should result in the InvalidReadOnlyUserDmlException
26 // being thrown.
27 insert testAccount2;
28 System.assertEquals(false, true);
29 } catch (System.InvalidReadOnlyUserDmlException e) {
30 // Expected
31 }
32 // Insertion should work after read only application mode gets disabled.
33 Test.setReadOnlyApplicationMode(false);
34
35 insert testAccount2;
36 Account testAccount2FromDb =
37 [SELECT Id, Name FROM Account WHERE Name = 'TestAccount2'];
38 System.assertEquals(testAccount2.Id, testAccount2FromDb.Id);
39 }
40}testInstall(InstallHandler, Version, Boolean)
署名
public static Void testInstall(InstallHandler installImp, Version ver, Boolean isPush)
パラメータ
- installImp
- 型: System.InstallHandler
- InstallHandler インターフェースを実装するクラス
- ver
- 型: System.Version
- 登録者組織にインストールされた既存パッケージのバージョン番号を指定します。
- isPush
- 型: Boolean
- (省略可能) アップグレードがプッシュかどうかを指定します。デフォルト値は、false です。
戻り値
型: Void
例
1@isTest static void test() {
2 PostInstallClass postinstall =
3 new PostInstallClass();
4 Test.testInstall(postinstall,
5 new Version(1,0));
6 }testUninstall(UninstallHandler)
署名
public static Void testUninstall(UninstallHandler uninstImp)
パラメータ
- uninstImp
- 型: System.UninstallHandler
- UninstallHandler インターフェースを実装するクラス
戻り値
型: Void
例
1@isTest static void test() {
2 UninstallClass uninstall =
3 new UninstallClass();
4 Test.testUninstall(uninstall);
5 }