+ Start a Discussion
Yelper DevYelper Dev 

Test Class for Response HTTP

Do you have any idea on how we can include the code below in test class especially on messageErrorResp(); ?

Thank you.
 
public class headResponse {
        public String code { get; set; }
        public Integer httpStatus { get; set; }
        public String message { get; set; }
        public String moreInfo { get; set; }
        public String severity { get; set; }
        public String transId{ get; set; }
        
        public headResponse(){
            code = null;
            httpStatus = null;
            message = null;
            moreInfo = null;
            severity = null;
            transId = null;
        }
    }
 
 @TestVisible private void messageErrorResp(HttpResponse response){
        headResponse errorResponse;
        try{
            errorResponse = (headResponse)JSON.deserialize(response.getBody(),headResponse.class);
        } 
        catch(Exception e){
            // Could not parse the JSON response so likely a HTTP error (eg. timeout)
            throw new Exception('Call to backend services returned a ' + response.getStatusCode() + ' ' + response.getStatus());
        }   
        throw new Exception('Call to backend services returned ' + errorResponse.code + ' - ' + errorResponse.message);
    }

 
ShirishaShirisha (Salesforce Developers) 
Hi Yelper,

Greetings!

Please find the sample code for the test class to test the http callouts:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri