---
schema_version: "1"
id: 3486a15982f305f0002087ac96adb8f5caab5f8dfc77e94a369699633df7f67a
title: Create Unit Testing for Asynchronous Callouts
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/platform/264-0-0/external_services_aysnc_ops_testing.md
canonical_url: https://help.salesforce.com/s/articleView?id=platform.external_services_aysnc_ops_testing.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 934d761eb594b7157436d207d0576b5dc163ebe270cfc0898e9bbcf11eba79ef
description: You can use the ExternalServiceTest method to mock callback and asynchronous responses.
products:
  - Salesforce Platform
  - Security
release: "264"
---
# Create Unit Testing for Asynchronous Callouts

You can use the `ExternalServiceTest` method to mock callback and asynchronous responses.

### Required Editions

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition"><colgroup><col></colgroup><tbody><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: Lightning Experience</td></tr><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: <strong>Enterprise</strong>, <strong>Performance</strong>, <strong>Unlimited</strong>, and <strong>Developer</strong> Editions</td></tr></tbody></table>

The code examples in this topic are based on the fictitious schema in [Example API Specification With Callback Operation](https://help.salesforce.com/s/articleView?id=platform.external_services_async_ops_example.htm&release=264.0.0&type=5).

This snippet shows an example unit test for the Acme Mortgage service. To test external services with callbacks in Apex, the Acme Mortgage callout mock class mocks the submit-mortgage application request and asserts the expected request payload. The test class triggers the mocked external service’s callback response through an instance of test External Service methods.

```java
global class AcmeMortgageHttpCalloutMock implements HttpCalloutMock {
    private String callbackUrl;
    
    // Mock asynchronous response
    public void callBack() {
        HttpRequest request = new HttpRequest();
        request.setEndpoint(callbackUrl);
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json');
        request.setBody('{' + 
                            '"applicationNumber": "900-X",' + 
                            '"status": "Approved",' +
                            '"approvedAmount": 10000' + 
                        '}');
                        
        HttpResponse response = Test.getExternalService().sendCallback(request);
        Assert.areEqual(200, response.getStatusCode());
    }
          
    // Mock initial response
    global HttpResponse respond(HttpRequest request) {
        if (request.getMethod().equals('POST') && request.getEndpoint().equals('callout:AcmeMortgageCredential/applications')) {
            return respondSubmitApplication(request);
        }
        ...
        System.assert(false, 'Request method and/or endpoint not valid: ' + request.getMethod() + ' ' + request.getEndpoint());
        return null;
    }
    
    private HttpResponse respondSubmitApplication(HttpRequest request) {
        // Retrieve the callback URL from request
        ExternalService.AcmeMortgage_SubmitApplication_IN_Body body = 
(ExternalService.AcmeMortgage_SubmitApplication_IN_Body) 
                                     Json.deserialize(request.getBody(), 
(ExternalService.AcmeMortgage_SubmitApplication_IN_Body.class);
        callbackUrl = body.callbackUrlForOutcomes.approved;
        
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{"applicationNumber": "900-X"}');
        response.setStatusCode(201);
        return response;
    }
}
```

The test class sets up the callout mock, sends the mortgage application request, and then mocks the callback with the mortgage application outcome:

```
@IsTest
public class AcmeMortgageApplicationTest {
    @IsTest
    static void testSubmitApplicationWithApprovalCallback() {
        AcmeMortgageHttpCalloutMock calloutMock = new AcmeMortgageHttpCalloutMock();
        Test.setMock(HttpCalloutMock.class, calloutMock);
        Test.startTest();
        
        ExternalService.AcmeMortgage acme = new ...();
        ...acme.SubmitApplication(...);
        
        // Call back with mock responses
        calloutMock.callBack();
        // Send all registered callback
        Test.stopTest();
        
        // Assert expected end result - e.g approved mortgage object created
        ...
    }
}

  
```