0

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

1
  • 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, 2020 at 4:28

1 Answer 1

0

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
      con.continuationMethod='processInitialResponse';

      // Create first callout request
      HttpRequest req = new HttpRequest();
      req.setMethod('GET');
      req.setEndpoint(LONG_RUNNING_SERVICE_URL1);

      // 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
      chainedContinuation.continuationMethod='processChainedResponse';

      // Create callout request
      HttpRequest req = new HttpRequest();
      req.setMethod('GET');
      req.setEndpoint(LONG_RUNNING_SERVICE_URL2);

      // 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.

 @isTest
  private static void continuationChainedTestMethod() {

    HttpResponse response1 = new HttpResponse();
    HttpResponse response2 = new HttpResponse();
    response1.setBody('{ "success":true}');
    response1.setStatusCode(200);
    response2.setBody('{ "success":true}');
    response2.setStatusCode(200);
      Test.startTest();
      // 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(),
        con
      );
      String response = (String) Test.invokeContinuationMethod(
        new ChainedContinuationController(),
        conChained1
      );
      Test.stopTest();
      // Assert Statements goes here
  }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .