この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

非同期コールアウトのテスト

コントローラをテストし、Apex のリリースまたはパッケージ化のコードカバー率要件を満たすためのテストを記述します。Apex テストはコールアウトの実行をサポートしていないため、コールアウトの要求と応答をシミュレーションできます。コールアウトをシミュレーション中に、要求が外部サービスに送信されることはなく、疑似応答が使用されます。

次の例は、HTTPRequest を使用する Web サービスコールのテストで擬似非同期コールアウトを呼び出す方法を示しています。継続でコールアウトをシミュレーションするには、Test クラスの setContinuationResponse(String, System.HttpResponse) メソッドと invokeContinuationMethod(Object, Continuation) メソッドをコールします。

最初にテストするコントローラクラス、続いてテストクラスをリストします。Visualforce ページでの長時間コールアウトの実行のコントローラクラスは、ここで再度使用されます。

1public with sharing class ContinuationController {
2    // Unique label corresponding to the continuation request
3    public String requestLabel;
4    // Result of callout
5    public String result {get;set;}
6    // Endpoint of long-running service
7    private static final String LONG_RUNNING_SERVICE_URL = 
8        '<Insert your service URL>';
9   
10   // Action method
11    public Object startRequest() {
12      // Create continuation with a timeout
13      Continuation con = new Continuation(40);
14      // Set callback method
15      con.continuationMethod='processResponse';
16      
17      // Create callout request
18      HttpRequest req = new HttpRequest();
19      req.setMethod('GET');
20      req.setEndpoint(LONG_RUNNING_SERVICE_URL);
21      
22      // Add callout request to continuation
23      this.requestLabel = con.addHttpRequest(req);
24      
25      // Return the continuation
26      return con;  
27    }
28    
29    // Callback method 
30    public Object processResponse() {   
31      // Get the response by using the unique label
32      HttpResponse response = Continuation.getResponse(this.requestLabel);
33      // Set the result variable that is displayed on the Visualforce page
34      this.result = response.getBody();
35      
36      // Return null to re-render the original Visualforce page
37      return null;
38    }
39}

次の例は、コントローラに対応するテストクラスを示しています。このテストクラスには、非同期コールアウトをテストするテストメソッドが含まれます。このテストメソッドで、Test.setContinuationResponse によって擬似応答が設定され、Test.invokeContinuationMethod によって継続のコールバックメソッドが実行されます。テストでは、コントローラの結果変数が予期される応答に設定されたことを検証し、コールバックメソッドで擬似応答が処理されたことを確認します。

1@isTest
2public class ContinuationTestingForHttpRequest {
3    public static testmethod void testWebService() {
4        ContinuationController controller = new ContinuationController();
5        // Invoke the continuation by calling the action method
6        Continuation conti = (Continuation)controller.startRequest();
7        
8        // Verify that the continuation has the proper requests
9        Map<String, HttpRequest> requests = conti.getRequests();
10        system.assert(requests.size() == 1);
11        system.assert(requests.get(controller.requestLabel) != null);
12        
13        // Perform mock callout 
14        // (i.e. skip the callout and call the callback method)
15        HttpResponse response = new HttpResponse();
16        response.setBody('Mock response body');   
17        // Set the fake response for the continuation     
18        Test.setContinuationResponse(controller.requestLabel, response);
19        // Invoke callback method
20        Object result = Test.invokeContinuationMethod(controller, conti);
21        // result is the return value of the callback
22        System.assertEquals(null, result);
23        // Verify that the controller's result variable
24        //   is set to the mock response.
25        System.assertEquals('Mock response body', controller.result);
26    }
27}