function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
guptach45guptach45 

Test class for controller.PLease Help

public class DivAC_14_IntegrationController {
   
    public static HttpResponse makeCallOut(String requestBody,String END_POINT){
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(END_POINT);
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setMethod('POST');
        request.setBody(requestBody);
        request.setTimeout(45000);
        HttpResponse response = http.send(request);
        // If the request is successful, parse the JSON response.
        if (response.getStatusCode() == 200) {
            system.debug('body--->'+response.getBody());
        }else{
            system.debug('response--->'+response);
        }
        return response;
    }
    
}
Best Answer chosen by guptach45
ANUTEJANUTEJ (Salesforce Developers) 
Hi Sakshi,

You could test the callouts in two ways as mentioned in https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing.htm this article, you could choose one to cover the class method.

Please find an example below for quick reference that is mentioned in the article:
 
public class CalloutClass {
    public static HttpResponse getInfoFromExternalService() {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://example.com/example/test');
        req.setMethod('GET');
        Http h = new Http();
        HttpResponse res = h.send(req);
        return res;
    }
}

Test Class:
 
@isTest
global class MockHttpResponseGenerator implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {
        // Optionally, only send a mock response for a specific endpoint
        // and method.
        System.assertEquals('http://example.com/example/test', req.getEndpoint());
        System.assertEquals('GET', req.getMethod());
        
        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"example":"test"}');
        res.setStatusCode(200);
        return res;
    }
}

I hope this helps in having a reference to implement for your usecase, do let me know if there are any question to assist further, in case if this came in handy for your implementation could you please mark this as best answer so that it could be used by others in the future.

Regards,
Anutej