18

With Summer'19 release continuation is available in Lightning. To make long running callouts.

Continuation - Aura Dev Guide: https://developer.salesforce.com/docs/atlas.en-us.220.0.lightning.meta/lightning/apex_continuations.htm?search_text=COntinuation

Documentation only provides how to use continuation in Apex Controller. But it does not provide any sample code for Unit Test Coverage.

As per the documentation the callback method should be static with list of labels and state as arguments. But if we use the existing testing framework for continuation test coverage. If we pass object of Controller in ‘invokeContinuationMethod’ it throws error that callback method does not exist…because callback method is static and object of controller can not call static method…then what is workaround?

Here is the similar stack exchange question which states it as testing framework gap: How to create controller class instance to pass it as argument of 'Test.invokeContinuationMethod' while Callback method is Static

Apex Class:

public class ContinuationSampleCode {
private static final String LONG_RUNNING_SERVICE_URL ='callout:MockService';

// Action method
@AuraEnabled(continuation=true cacheable=true)
public static Object startRequest() {
  // Create continuation. Argument is timeout in seconds.
  Continuation con = new Continuation(40);
  // Set callback method
  con.continuationMethod='processResponse';
  // Set state
  con.state='Hello, World!';
  // Create callout request
  HttpRequest req = new HttpRequest();
  req.setMethod('GET');
  req.setEndpoint(LONG_RUNNING_SERVICE_URL);
  // Add callout request to continuation
  con.addHttpRequest(req);
  // Return the continuation
  return con;
}

// Callback method
@AuraEnabled(cacheable=true)
public static Object processResponse(List<String> labels, Object state) {
  // Get the response by using the unique label
  HttpResponse response = Continuation.getResponse(labels[0]);
  // Set the result variable
  String result = response.getBody();
  return result;
}}

Apex Test Class:

@isTest private class ContinuationSampleCode_Test {
@isTest private static void startRequestTest() {
        Test.startTest();
        HttpResponse response = new HttpResponse();
        response.setBody('Mock response body');   
        Continuation conti = (Continuation)ContinuationSampleCode.startRequest();
            // Set the fake response for the continuation     
            Test.setContinuationResponse('continuation-1', response);
            // Invoke callback method
            Object result = Test.invokeContinuationMethod(new ContinuationSampleCode(), conti);    
        Test.stopTest();
        system.assert(result!=null);
}
}

Already created with SFDC Support. Waiting for response from them.

Looking for solution on this. Appreciate your time and help.

  • Yes...@harsha still did not get any response from salesforce on that ticket :) – Ayub May 13 '19 at 18:28
  • I may be missing something, but I'm not certain how this can be much different from doing the same thing using javascript remoting as described in the Apex Docs at the bottom of Process for Using Asynchronous Callouts. I'd expect the test method to look the same. – crmprogdev Jun 23 '19 at 18:51
1

I tried making few changes to your code and this may be a solution although it does require adding an additional method in your main class.

Apex Class :

    public class ContinuationSampleCode {
    private static final String LONG_RUNNING_SERVICE_URL ='callout:MockService';
    // Action method
    @AuraEnabled(continuation=true cacheable=true)
    public static Object startRequest() {
      // Create continuation. Argument is timeout in seconds.
      Continuation con = new Continuation(40);
      // Set callback method
      con.continuationMethod='processResponse';
      // Set state
      con.state='Hello, World!';
      // Create callout request
      HttpRequest req = new HttpRequest();
      req.setMethod('GET');
      req.setEndpoint(LONG_RUNNING_SERVICE_URL);
      // Add callout request to continuation
      con.addHttpRequest(req);
      // Return the continuation
      return con;
    }

    // Callback method
    @AuraEnabled(cacheable=true)
    public static Object processResponse(List<String> labels, Object state) {
      // Get the response by using the unique label
      HttpResponse response = Continuation.getResponse(labels[0]);
      // Set the result variable
      String result = response.getBody();
      return result;
    }


    public static Object processResponse() {
        List<String> labels = new list<String>();
        labels.add('continuation-1');
        object result =  processResponse(labels,null);
        return result;
    }

    }

Test Class // test class

    @isTest private class ContinuationSampleCode_Test {
    @isTest private static void startRequestTest() {
            Test.startTest();
            HttpResponse response = new HttpResponse();
            response.setBody('Mock response body');   
            Continuation conti = (Continuation)ContinuationSampleCode.startRequest();
                // Set the fake response for the continuation     
                Test.setContinuationResponse('continuation-1', response);
                // Invoke callback method
                Object result = Test.invokeContinuationMethod(new ContinuationSampleCode(), conti);    
                Test.stopTest();
            system.assert(result!=null);
    }
    }
| improve this answer | |
0

Just to cover some parts of the code, you can pass the processResponse() in the argument just like this:

    try{
    Object result = Test.invokeContinuationMethod(ContinuationSampleCode.processResponse(new List<String>{ContinuationSampleCode.requestLabel}, testObj), conti);
    }

Catch(Exception exc){
    //any statement
}

Here 'testObj' is any dummy/actual object/wrapper/inner class from your ContinuationSampleCode class. It will thrown an error which will be caught in catch block.

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