外部変更データキャプチャパッケージとテスト
Apex トリガーをテストするためのフレームワークを含む管理パッケージで外部変更データキャプチャコンポーネントを配布できます。パッケージ化とパッケージのインストールには特殊な動作と制限が適用されます。
- Apex クラスコンポーネントの種類リストからテストを選択して、外部変更データ追跡コンポーネントを管理パッケージに含めます。トリガー、テスト、外部データソース、外部オブジェクト、その他の関連アセットは、配布用のパッケージに取り込まれます。
- 証明書はパッケージ化できません。証明書を指定する外部データソースをパッケージ化する場合は、同じ名前の有効な証明書が登録者組織に存在することを確認してください。
外部変更データキャプチャ (トリガーされた Apex クラス) のテストに役立つように、シミュレートされた外部変更に反応するトリガーの単体テストコードの例を次に示します。
トリガーの例
1trigger OnExternalProductChangeEventForAudit on Products__ChangeEvent (after insert) {
2 if (Trigger.new.size() != 1) return;
3 for (Products__ChangeEvent event: Trigger.new) {
4 Product_Audit__c audit = new Product_Audit__c();
5 audit.Name = 'ProductChangeOn' + event.ExternalId;
6 audit.Change_Type__c = event.ChangeEventHeader.getChangeType();
7 audit.Audit_Price__c = event.Price__c;
8 audit.Product_Name__c = event.Name__c;
9 insert(audit);
10 }
11}
Apex テスト
1@isTest
2public class testOnExternalProductChangeEventForAudit {
3 static testMethod void testExternalProductChangeTrigger() {
4 // Create Change Event
5 Products__ChangeEvent event = new Products__ChangeEvent();
6 // Set Change Event Header Fields
7 EventBus.ChangeEventHeader header = new EventBus.ChangeEventHeader();
8 header.changeType='CREATE';
9 header.entityName='Products__x';
10 header.changeOrigin='here';
11 header.transactionKey = 'some';
12 header.commitUser = 'me';
13 event.changeEventHeader = header;
14 event.put('ExternalId', 'ParentExternalId');
15 event.put('Price__c', 5500);
16 event.put('Name__c', 'Coat');
17 // Publish the event to the EventBus
18 EventBus.publish(event);
19 Test.getEventBus().deliver();
20 // Perform assertion that the trigger was run
21 Product_Audit__c audit = [SELECT name, Audit_Price__c, Product_Name__c FROM Product_Audit__c WHERE name = : 'ProductChangeOn'+ event.ExternalId LIMIT 1];
22 System.assertEquals('ProductChangeOn'+ event.ExternalId, audit.Name);
23 System.assertEquals(5500, audit.Audit_Price__c);
24 System.assertEquals('Coat', audit.Product_Name__c);
25 }
26}