0

I have the apex class that makes the call to two endpoints in the same method like below

@future (callout=true)
public static void createSegSubSegment(String jsonBody,String endPoint) {
    String clientId = 'xxxxxx';
    String clientSecret = 'xxxxx';
    String resource = 'xxxxxx';
    String tenant_id = 'xxxxx';
    String bearerToken = '';        
    try{                       
        String reqbody = 'grant_type=client_credentials&client_id='+clientId+'&client_secret='+clientSecret+'&tenant_id='+tenant_id+'&resource='+resource;

            Http h = new Http();
            HttpRequest req = new HttpRequest();
            req.setBody(reqbody);
            req.setMethod('POST');
            req.setEndpoint('https://..........');
            HttpResponse res = h.send(req);

            if(res.getstatusCode() == 200 && res.getbody() != null){
            deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
            bearerToken = resp1.access_token;
            }

            Http http1 = new Http();
            HttpRequest req1 = new HttpRequest();
            req1.setMethod('POST');
            req1.setTimeout(20000);
            req1.setHeader('Authorization','Bearer '+bearerToken);
            req1.setEndpoint('https://..........');
            req1.setBody(jsonBody);
            req1.setHeader('Content-Type', 'application/json;charset=UTF-8');
            HttpResponse res1 = http1.send(req1);
         }
    } 

I was able to make the mock call to the 1st endpoint from my test class like below

@IsTest
public class calloutTest {
    private static testMethod void callFutureMethod() {      
        .......
        .......

        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new HttpMockCalloutTest());
        handleD365Request.createSegSubSegment(jsonBody,endPoint);
        Test.stopTest();

    }

Where HttpMockCalloutTest is like below

@isTest
global class HttpMockCalloutTest implements HttpCalloutMock{
    global HttpResponse respond(HTTPRequest req){
        HttpResponse res = new HttpResponse();
        res.setStatus('OK');
        res.setStatusCode(200);
        res.setBody('Test Mock Callout');
        return res;
    }
}

How can I make the mock call to the other endpoint.I am little confused as I have the endpoints are called from the same method in the original apex class

2
  • I linked an answer of mine that should get you where you want to go. Please let me know if this doesn't help, or you need further assistance, we'll be glad to reopen this question once you make an edit to include what attempts you've made. – sfdcfox Jun 3 '20 at 0:01
  • Here is an adaptation I made as well. Handles in a different way and allows more varied setup. Just in case you find it useful but it is more complex than linked answer and I may need to review the functionality gaps if any and may change to the linked answer salesforce.stackexchange.com/questions/228369/… – Eric Jun 3 '20 at 7:19