Im wondering if anyone has figured out a way to test chained continuations? I followed the documentation for how to essentially make two asynchronous callouts in a synchronous pattern. I can test the first continuation just fine but I am struggling to find a way to invoke the second callout in a test setting.

Salesforce Documentation

Chaining Asynchronous Callouts

Other unanswered Questions:

Test class for continuation chaining

Continuation obj null in setContinuationMethod

  • The other questions like this are years old and are unanswered from what I can find. If there is something i didnt see please post it. Thanks! – Jonathan Apr 18 at 4:28

Let's assume below is the sample apex continuation class, where you have 2 levels of continuation chaining.

public with sharing class ChainedContinuationController {

    // Unique label for the initial callout request
    public String requestLabel1;
    // Unique label for the chained callout request
    public String requestLabel2;
    // Result of initial callout
    public String result1 {get;set;}
    // Result of chained callout
    public String result2 {get;set;}
    // Endpoint 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 invokeInitialRequest() {
      // Create continuation with a timeout
      Continuation con = new Continuation(60);
      // Set callback method

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

      // Add initial callout request to continuation
      this.requestLabel1 = con.addHttpRequest(req);              

      // Return the continuation
      return con;  

    // Callback method for initial request
    public Object processInitialResponse() {   
      // Get the response by using the unique label
      HttpResponse response = Continuation.getResponse(this.requestLabel1);
      // Set the result variable that is displayed on the Visualforce page
      this.result1 = response.getBody();

      // Create a second continuation 
      Continuation chainedContinuation = new Continuation(60);
      // Set callback method

      // Create callout request
      HttpRequest req = new HttpRequest();

      // Add callout request to continuation
      this.requestLabel2 = chainedContinuation.addHttpRequest(req); 

      // Start another continuation 
      return chainedContinuation;  

    // Callback method for chained request
    public Object processChainedResponse() {   
      // Get the response for the chained request
      HttpResponse response = Continuation.getResponse(this.requestLabel2);
      // Set the result variable that is displayed on the Visualforce page
      this.result2 = response.getBody();

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

The Apex Test Method should be something like this.

  private static void continuationChainedTestMethod() {

    HttpResponse response1 = new HttpResponse();
    HttpResponse response2 = new HttpResponse();
    response1.setBody('{ "success":true}');
    response2.setBody('{ "success":true}');
      // Set the mock responses for the continuation
      Test.setContinuationResponse('Continuation-1', response1);
      Test.setContinuationResponse('Continuation-2', response2);
      Continuation con = (Continuation) ChainedContinuationController.invokeInitialRequest();
      Continuation conChained1 = (Continuation) Test.invokeContinuationMethod(
        new ChainedContinuationController(),
      String response = (String) Test.invokeContinuationMethod(
        new ChainedContinuationController(),
      // Assert Statements goes here
| improve this answer | |

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.