In Salesforce development, test methods don’t support web service callouts by default, and tests that perform web service callouts fail. When testing Apex code that makes HTTP callouts, Salesforce imposes restrictions that prevent actual callouts from being made in a test environment.
To bypass this limitation, we can create a mock callout. Mock callouts allow us to simulate HTTP responses, making it possible to test our code without making real Http requests.
Mock callout to test the Apex rest callout in Salesforce
To mock a callout, first, we will create an Apex class that makes a REST callout.
Apex Class code:
public class MyApiCallout {
public static String fetchDataFromAPI() {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://salesforcefaqs.com/apex/');
request.setMethod('GET');
request.setHeader('Content-Type', 'application/json');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
return response.getBody();
} else {
return 'Error: ' + response.getStatus();
}
}
}After this, we will create the Mock Response Class. In Salesforce, we have a “HttpCalloutMock” interface, which we will implement to mock an HTTP response. To mock a successful callout, we must create a class that mocks the HTTP response.
Apex Class code:
@isTest
public class MyApiCalloutMock implements HttpCalloutMock {
public HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HttpResponse();
res.setStatusCode(200);
res.setHeader('Content-Type', 'application/json');
// Mock Response Body
String mockResponseBody = '{"id": 563, "name": "Colorado Machines"}';
res.setBody(mockResponseBody);
return res;
}
}In this mock class, we have implemented the respond method, which creates a HttpResponse with a status code of 200 and a mock JSON response body.
Now, we will create a test class that uses the mock callout to test the MyApiCallout.fetchDataFromAPI() method.
Test class to test the Callout:
@isTest
public class MyApiCalloutTest {
@isTest
public static void testFetchDataFromAPI() {
System.Test.setMock(HttpCalloutMock.class, new MyApiCalloutMock());
String result = MyApiCallout.fetchDataFromAPI();
System.assertEquals('{"id":563, "name": "Colorado machines"}', result);
}In this test class, we have used Test.setMock() to bind the mock response to the callout. Finally, we use System.assertEquals() to verify that the method returns the expected mock response. We then call the fetchDataFromAPI method, which internally makes the HTTP request.
Finally, we will run the test class, and for that, select Test > New Run in the developer console and select the test class, then click on the Run button.

Output:

In the test run, we can see that the test run was successful. If you see any failures, check the mock response setup and ensure it matches your test’s expected output.
Error handling during the Mock Callout test
In the above example, I faced an error while creating the mock test class, and the error message appeared as “Method does not exist or incorrect signature in Test.setMock“. After finding a fix to this error, I found that this was a sort of hiding problem, where instead of “Test” referencing the Salesforce class, something else, like a local class called Test, is being referenced.
When I fixed the code with “System.Test.setMock“, the issue was resolved.
Conclusion
In this Salesforce tutorial, we have learned to test Apex code with HTTP callouts, which we have done effectively using mock callouts. By implementing the HttpCalloutMock interface, we can mock HTTP responses in tests, ensuring code functionality works without making real requests.
By following the above methods and code syntax, you can efficiently create a mock callout to test the Apex REST callout in Salesforce.
You may also like to read:
- Dependency Injection in Salesforce Apex Class
- Access Modifiers in Salesforce Apex Classes
- Test Apex Classes Using Test Methods in Salesforce
- Get User Or Logged-In User Details Using SOQL
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.