Write test class and meet the test code coverage for deploy or managed package. Since apex tests doesnot supports the callouts. We use mock responses.
To simulate callouts in continuations, call these methods of the Test class:
setContinuationResponse(requestLabel, mockResponse) and
invokeContinuationMethod(controller, request).
public with sharing class ContinuationController { // Unique label corresponding to the continuation request public String requestLabel; // Result of callout public String result {get;set;} // Endpoint of long-running service private static final String LONG_RUNNING_SERVICE_URL = 'This example shows the test class corresponding to the controller. This test class contains a test method for testing an asynchronous callout. In the test method, Test.setContinuationResponse sets a mock response, andTest.invokeContinuationMethod causes the callback method for the continuation to be executed. The test ensures that the callback method processed the mock response by verifying that the controller’s result variable is set to the expected response.'; // Action method public Object startRequest() { // Create continuation with a timeout Continuation con = new Continuation(40); // Set callback method con.continuationMethod='processResponse'; // Create callout request HttpRequest req = new HttpRequest(); req.setMethod('GET'); req.setEndpoint(LONG_RUNNING_SERVICE_URL); // Add callout request to continuation this.requestLabel = con.addHttpRequest(req); // Return the continuation return con; } // Callback method public Object processResponse() { // Get the response by using the unique label HttpResponse response = Continuation.getResponse(this.requestLabel); // Set the result variable that is displayed on the Visualforce page this.result = response.getBody(); // Return null to re-render the original Visualforce page return null; } }
@isTest public class ContinuationTestingForHttpRequest { public static testmethod void testWebService() { ContinuationController controller = new ContinuationController(); // Invoke the continuation by calling the action method Continuation conti = (Continuation)controller.startRequest(); // Verify that the continuation has the proper requests Maprequests = conti.getRequests(); system.assert(requests.size() == 1); system.assert(requests.get(controller.requestLabel) != null); // Perform mock callout // (i.e. skip the callout and call the callback method) HttpResponse response = new HttpResponse(); response.setBody('Mock response body'); // Set the fake response for the continuation Test.setContinuationResponse(controller.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 body', controller.result); } }
this is same as in https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_continuation_testing.htm
ReplyDeleteHi,I have used the above logic to cover continuation class but getting error like 'System.TypeException: Methods defined as TestMethod do not support Web service callouts'.I am calling continuation method using VF remoting in lightning component.Please help as i am not able to cover it.
ReplyDeleteAdd a mock HTTP Callout - a class which implements HttpCalloutMock and set the mock from your test class.
ReplyDelete