Apex-testing

提供:Dev Guides
移動先:案内検索

Apex-テスト

テストは、Apexまたはその他のアプリケーション開発の統合された部分です。 Apexには、すべてのユニットテスト用に開発する個別のテストクラスがあります。

テストクラス

SFDCでは、実稼働環境に展開するためにコードのコードカバレッジが75%でなければなりません。 このコードカバレッジは、テストクラスによって実行されます。 テストクラスは、他のApexクラスの機能をテストするコードスニペットです。

以前に記述したコードの1つのテストクラスを記述しましょう。 トリガークラスとヘルパークラスのコードをカバーするテストクラスを作成します。 以下は、カバーする必要があるトリガーおよびヘルパークラスです。

//Trigger with Helper Class
trigger Customer_After_Insert on APEX_Customer__c (after update) {
   CustomerTriggerHelper.createInvoiceRecords(Trigger.new, trigger.oldMap);
     //Trigger calls the helper class and does not have any code in Trigger
}

//Helper Class:
public class CustomerTriggerHelper {
   public static void createInvoiceRecords (List<apex_customer__c>

      customerList, Map<id, apex_customer__c> oldMapCustomer) {
      List<apex_invoice__c> InvoiceList = new List<apex_invoice__c>();

      for (APEX_Customer__c objCustomer: customerList) {
         if (objCustomer.APEX_Customer_Status__c == 'Active' &&
            oldMapCustomer.get(objCustomer.id).APEX_Customer_Status__c == 'Inactive') {

           //condition to check the old value and new value
            APEX_Invoice__c objInvoice = new APEX_Invoice__c();
            objInvoice.APEX_Status__c = 'Pending';
            objInvoice.APEX_Customer__c = objCustomer.id;
            InvoiceList.add(objInvoice);
         }
      }
      insert InvoiceList; //DML to insert the Invoice List in SFDC
   }
}

テストクラスの作成

このセクションでは、テストクラスを作成する方法を理解します。

データ作成

テストクラス自体にテストクラスのデータを作成する必要があります。 デフォルトでは、テストクラスは組織データにアクセスできませんが、@ isTest(seeAllData = true)を設定すると、組織のデータにもアクセスできるようになります。

@isTestアノテーション

このアノテーションを使用して、これはテストクラスであり、組織の合計コード制限に対してカウントされないことを宣言しました。

testMethodキーワード

ユニットテストメソッドは、引数を取らず、データベースにデータをコミットせず、電子メールを送信せず、メソッド定義でtestMethodキーワードまたはisTestアノテーションで宣言されているメソッドです。 また、テストクラス、つまりisTestアノテーションが付けられたクラスでテストメソッドを定義する必要があります。

例では「myUnitTest」テストメソッドを使用しました。

Test.startTest()およびTest.stopTest()

これらは、テストクラスで使用できる標準のテストメソッドです。 これらのメソッドには、テストをシミュレートするイベントまたはアクションが含まれています。 この例のように、トリガーとヘルパークラスをテストして、ブロックを開始および停止するために行ったようにレコードを更新することにより、起動トリガーをシミュレートします。 また、これにより、開始ブロックと停止ブロックにあるコードに個別のガバナー制限が提供されます。

System.assert()

このメソッドは、実際の目的の出力をチェックします。 この場合、請求書レコードが挿入されることを想定しているため、同じことを確認するためにassertを追加しました。

/**
* This class contains unit tests for validating the behavior of Apex classes
* and triggers.
*
* Unit tests are class methods that verify whether a particular piece
* of code is working properly. Unit test methods take no arguments,
* commit no data to the database, and are flagged with the testMethod
* keyword in the method definition.
*
* All test methods in an organization are executed whenever Apex code is deployed
* to a production organization to confirm correctness, ensure code
* coverage, and prevent regressions. All Apex classes are
* required to have at least 75% code coverage in order to be deployed
* to a production organization. In addition, all triggers must have some code coverage.
*
* The @isTest class annotation indicates this class only contains test
* methods. Classes defined with the @isTest annotation do not count against
* the organization size limit for all Apex scripts.
*
* See the Apex Language Reference for more information about Testing and Code Coverage.
*/

@isTest
private class CustomerTriggerTestClass {
   static testMethod void myUnitTest() {
     //Create Data for Customer Objet
      APEX_Customer__c objCust = new APEX_Customer__c();
      objCust.Name = 'Test Customer';
      objCust.APEX_Customer_Status__c = 'Inactive';
      insert objCust;

     //Now, our trigger will fire on After update event so update the Records
      Test.startTest();   //Starts the scope of test
      objCust.APEX_Customer_Status__c = 'Active';
      update objCust;
      Test.stopTest();    //Ends the scope of test

     //Now check if it is giving desired results using system.assert
     //Statement.New invoice should be created
      List<apex_invoice__c> invList = [SELECT Id, APEX_Customer__c FROM
         APEX_Invoice__c WHERE APEX_Customer__c = :objCust.id];
      system.assertEquals(1,invList.size());
     //Check if one record is created in Invoivce sObject
   }
}

テストクラスの実行

テストクラスを実行するには、以下の手順に従ってください-

  • ステップ1 *-Apexクラスに移動⇒クラス名「CustomerTriggerTestClass」をクリックします。
  • ステップ2 *-図のように[テストの実行]ボタンをクリックします。

頂点テストステップ1

  • ステップ3 *-ステータスを確認する

Apex Testing Step2

  • ステップ4 *-テストを作成したクラスとトリガーを確認します

クラス

Apex Testing Step3

引き金

Apex Testing Step4

テストは成功して完了しました。