Salesforce | Methods defined as TestMethod do not support Web service callouts

|
| By Webner

Our code has some classes which contain functions that are used to make webservice callouts to connect with AWS S3 or EC2 machine. We have to cover these functions in our test methods but “Run Test” fails after running the first function with following message:

(Older API) Methods defined as TestMethod do not support Web service callouts, test skipped

Since in the past salesforce used to skip such methods there was no issue. But now new Salesforce API started throwing following error:

(NEW API – 15) System.TypeException: Testmethods do not support webservice callouts.

Solution : Using following code in our test class we were able to resolve this issue:

StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
mock.setStaticResource('NameOfStaticResourceContainingResponseBodyString');
mock.setStatusCode(200); // Or other appropriate HTTP status code
mock.setHeader('Content-Type', 'application/json'); // Or other appropriate MIME type like application/xml
Test.setMock(HttpCalloutMock.class, mock);

Here is the section that covers this topic in detail:

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

Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Salesforce customization, App development or any other software development assistance please contact us at salesforce@webners.com

Leave a Reply

Your email address will not be published. Required fields are marked *