+ Start a Discussion
sumit dsumit d 

Test class for API callout

Hello All,
                I want to create a test class for a class in which I used callouts.
Can anyone help me how to write test class for API?
my class is given below:-
public without sharing class ZoomCalloutService {
        //Get Request
    public static httpResponse getCallout(String httpMethod, String endpoint){
        httpRequest req = new httpRequest();
        req.setMethod(httpMethod);
        req.setEndpoint(endpoint);
        req.setHeader('Content-type', 'application/json');
        if(ZoomJWTAuthentication.authToken != Null){
            req.setHeader('Authorization','Bearer '+ZoomJWTAuthentication.authToken);
        }else{
            req.setHeader('Authorization','Bearer ' +getAccessToken());
        }
        System.debug(ZoomJWTAuthentication.authToken);    
        req.setTimeout(120000);
        httpResponse res = new http().send(req);
        if(res.getStatusCode() == 401){
            req.setHeader('Authorization','Bearer '+getAccessToken());
            httpResponse res1 = new http().send(req);
            return res1;
        }
        return res;
        
    }
    
    //Post Request
    public static httpResponse postCallout(String httpMethod, String endpoint, String body){
        httpRequest req = new httpRequest();
        req.setMethod(httpMethod);
        req.setEndpoint(endpoint);
        req.setBody(body);
        req.setHeader('Content-type', 'application/json');
        if(ZoomJWTAuthentication.authToken != Null){
            req.setHeader('Authorization','Bearer '+ZoomJWTAuthentication.authToken);
        }else{
            req.setHeader('Authorization','Bearer ' +getAccessToken());
        }
        System.debug(ZoomJWTAuthentication.authToken);    
        req.setTimeout(120000);
        httpResponse res = new http().send(req);
        if(res.getStatusCode() == 401){
            req.setHeader('Authorization','Bearer '+getAccessToken());
            httpResponse res1 = new http().send(req);
            return res1;
        }
        return res;
        
    }
    
    public static String getAccessToken(){
        return ZoomJWTAuthentication.createTokenFromJWTAuth();
    }
}
Can anyone help me with the test class?
AbhishekAbhishek (Salesforce Developers) 
Hi,

You can use the code as mentioned in the below blog,

https://developer.salesforce.com/forums/?id=9062I000000g829QAA

https://salesforce.stackexchange.com/questions/139235/how-to-create-mock-class-for-multiple-callouts-in-single-class

It might help you.

 
Rahul Kumar DeyRahul Kumar Dey
Hi Sumit,

Here you can find- how to create mock interface and creating fake response for your test and writing test class for your http callout?
https://trailhead.salesforce.com/en/content/learn/modules/apex_integration_services/apex_integration_rest_callouts

If this solve your problem don't forgot to mark this best answer.
Regards,
Rahul