+ Start a Discussion
Anshul Kapoor 8Anshul Kapoor 8 

Technical concept of HttpCalloutMock.class

Hi All,

Test.setMock(HttpCalloutMock.class, new AnimalsHttpCalloutMock()); 
Please let me know the technical understanding of the HttpCalloutMock.class? I guess, it is interface.class / class.class. Where could we use the class.class or interface.class?
Best Answer chosen by Anshul Kapoor 8
ravi soniravi soni

Hi  Anshul,
go to referal url :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm
as suraj mentioned that's is the right.
and AnimalResult result = (AnimalResult) JSON.deserialize(response.getBody(), AnimalResult.class).
AnimalResult is the another class such as wrapper class , in where you are doing set response body.

for instance : 
public class AnimalResult{
public  string sResponse;
public string sData;
}
assume that you have a class in where you have 2 property like above when you execute following line the response data automaticly store into animalResult's property.
AnimalResult result = (AnimalResult) JSON.deserialize(response.getBody(), AnimalResult.class).
but make sure that the property name must be into impletemantion class's body.

for example following is your implement class and your property name must be into this class response body like following.

res.setBody('{"example":"test",
"sResponse" : "putValue",
"sData": "putData"}');

@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;
    }
}
let me know if it helps you and marking it as best answer
Thank you

All Answers

Anshul Kapoor 8Anshul Kapoor 8
One more statement: AnimalResult result = (AnimalResult) JSON.deserialize(response.getBody(), AnimalResult.class);
what does it mean by AnimalResult.class?
Suraj Tripathi 47Suraj Tripathi 47
Hi Anshul

Greetings!

Test.setMock is used to create a fake response.
It helps you to cover your code coverage in Test class.

In Another statement AnimalResult result = (AnimalResult) JSON.deserialize(response.getBody(), AnimalResult.class).,
In which, we parse our JSON Data into AnimalResult class instance.
Which we can insert or update in the apex.

Please mark it as the best answer if it helps you to fix the issue.
Thank you!
Regards,
Suraj Tripathi
Anshul Kapoor 8Anshul Kapoor 8
Hi Suraj,

I understand the Test.setMock is used to create a fake response. Could you explain the technical concept AnimalResult.class OR HttpCalloutMock.class. AnimalResult is the class, HttpCalloutMock is the interface.
ravi soniravi soni

Hi  Anshul,
go to referal url :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm
as suraj mentioned that's is the right.
and AnimalResult result = (AnimalResult) JSON.deserialize(response.getBody(), AnimalResult.class).
AnimalResult is the another class such as wrapper class , in where you are doing set response body.

for instance : 
public class AnimalResult{
public  string sResponse;
public string sData;
}
assume that you have a class in where you have 2 property like above when you execute following line the response data automaticly store into animalResult's property.
AnimalResult result = (AnimalResult) JSON.deserialize(response.getBody(), AnimalResult.class).
but make sure that the property name must be into impletemantion class's body.

for example following is your implement class and your property name must be into this class response body like following.

res.setBody('{"example":"test",
"sResponse" : "putValue",
"sData": "putData"}');

@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;
    }
}
let me know if it helps you and marking it as best answer
Thank you
This was selected as the best answer