I'm currently working on covering code coverage for continuation pattern. However, I receive an exception while testing. I have chained 3 webservices ie., service 1,2 and 3.

 private static testMethod void testContinuation1() {
 setAllData();// test data is set up here
  MainController Controller = new MainController();

    // Invoke the continuation by calling the action method
    Continuation conti = (Continuation)Controller.CallMainMethod();

    Map<String, HttpRequest> requests = conti.getRequests();

    HttpResponse response = new HttpResponse();
    HttpRequest request = new HttpRequest();

    for(String RequestLabel: requests.keyset()){
        request = requests.get(RequestLabel);
        if(request.getEndpoint().contains('Service1')) {
        else if(request.getEndpoint().contains('Service2')) {
        Test.setContinuationResponse(RequestLabel, response);                   
    system.assert(requests.size() == 3);

    if(conti != null){
      Object result = Test.invokeContinuationMethod(Controller, conti);

I'm not able to set the mock responses for each webservices. Rather, I receive an error as..

Argument 2 cannot be null.

in Test.invokeContinuationMethod. Also debug 'conti-->' has a callback method. Do I have to instantiate 3 different Continuation object to test 3 different webservices?

Suggestion would be appreciated. Thanks!


You don't have to instantiate 3 different Continuation objects to test 3 different webservices.

Only single instance of Continuation object will work.

I have tried to replicate your use case using Continuation creating multiple callouts

Controller (taken from Making Multiple Asynchronous Callouts)

public with sharing class MultipleCalloutController {

    // Unique label for the first request
    public String requestLabel1;
    // Unique label for the second request
    public String requestLabel2;
    // Result of first callout
    public String result1 {get;set;}
   // Result of second callout
    public String result2 {get;set;}
    // Endpoints of long-running service
    private static final String LONG_RUNNING_SERVICE_URL1 = 
        '<Insert your first service URL>';
    private static final String LONG_RUNNING_SERVICE_URL2 = 
        '<Insert your second service URL>';

    // Action method
    public Object startRequestsInParallel() {
      // Create continuation with a timeout
      Continuation con = new Continuation(60);
      // Set callback method

      // Create first callout request
      HttpRequest req1 = new HttpRequest();

      // Add first callout request to continuation
      this.requestLabel1 = con.addHttpRequest(req1);     

      // Create second callout request
      HttpRequest req2 = new HttpRequest();

      // Add second callout request to continuation
      this.requestLabel2 = con.addHttpRequest(req2);     

      // Return the continuation
      return con;  

    // Callback method.
    // Invoked only when responses of all callouts are returned.
    public Object processAllResponses() {   
      // Get the response of the first request
      HttpResponse response1 = Continuation.getResponse(this.requestLabel1);
      this.result1 = response1.getBody();

      // Get the response of the second request
      HttpResponse response2 = Continuation.getResponse(this.requestLabel2);
      this.result2 = response2.getBody();

      // Return null to re-render the original Visualforce page
      return null;

Test Class

public class MyContinuationTesting {
    public static testmethod void testWebService() {
        MultipleCalloutController controller = new MultipleCalloutController();
        // Invoke the continuation by calling the action method
        Continuation conti = (Continuation)controller.startRequestsInParallel();

        // Verify that the continuation has the proper requests
        Map<string, System.HttpRequest> requests = conti.getRequests();

        system.assert(requests.size() == 2);
        system.assert(requests.get(controller.requestLabel1) != null);

        // Perform mock callout 
        // (i.e. skip the callout and call the callback method)
        integer i = 1;
        for(String RequestLabel: requests.keyset())
            HttpResponse response = new HttpResponse();
            response.setBody('Mock response body' + i); 
            Test.setContinuationResponse(RequestLabel, response);
        // Invoke callback method
        Object result = Test.invokeContinuationMethod(controller, conti);
        // result is the return value of the callback
        System.assertEquals(null, result);
        // Verify that the controller's result variable
        //   is set to the mock response.
        System.assertEquals('Mock response body1', controller.result1);

        System.assertEquals('Mock response body2', controller.result2);
| improve this answer | |
  • Hi @santanu is the controller class covered by this test class? Because I too tried with the "i" variable and it didn't work as I won't know which service would be of which label. – MnZ May 21 '17 at 1:25
  • Are you getting the error which you were getting earlier? – Santanu Boral May 21 '17 at 1:47
  • I'm trying it out again. I will reply once I'm done – MnZ May 21 '17 at 1:49
  • So basically all the assert statements failed." I get this error--> External entry point Class.System.Test.invokeContinuationMethod" .The request labels for service1 and service2 are in 2 separate classes. – MnZ May 21 '17 at 2:29
  • I can't see your controller code so can't predict that, but the code example I have given is perfectly working for me – Santanu Boral May 21 '17 at 2:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.