I'm trying here to write a test class for below apex class, I'm getting the below error when in the compilation:

Methods defined as TestMethod do not support Web service callouts

Test class:

Public  class testPricingSalesforceController{

  @isTest static void testCallout() {
       // Call method to test.
        // This causes a fake response to be sent
         //Test.setMock(PricingSalesforceController.class, new testPricingSalesforceController());
         //Test.setMock(HttpCalloutMock.class, new YourHttpCalloutMockImpl());
             // from the class that implements HttpCalloutMock. 
        String res = PricingSalesforceController.reqprice('CM','NSA','222Y16C',2);

        //return res();
        // Verify response received contains fake values       
        //System.assertEquals('Mock response', res);
        // res=getbody();


Apex class

Public class PricingSalesforceController {
        public static string reqprice(string famil,string idcom,string partno,integer qty){
        HttpRequest req = new HttpRequest();
         // Specify the required user name and password to access the endpoint
         // As well as the header and header information

         String username = '000dwe000';
         String password = '0000%';

         Blob headerValue = Blob.valueOf(username + ':' + password);
         String authorizationHeader = 'BASIC ' +
         req.setHeader('Authorization', authorizationHeader);

         // Create a new http object to send the request object
         // A response object is generated as a result of the request  
         Http http = new Http();
         HTTPResponse res = http.send(req);
      //  String mapString = (res.getbody()).tostring();
        // system.debug(mapString);
         return  res.getbody(); 

         //Map < String,
            //Object > resultsMap = (Map < String, Object > ) JSON.deserializeUntyped(res.getBody());
        //  system.debug('resultsMap-->' + resultsMap);
            //return resultsMap;
  • Definitely a duplicate, but look at the second answer on that linked question regarding callout mocks - preferable to using Test.isRunningTest() – David Reed Sep 5 '18 at 13:12

You can do it in two ways :

  • Test a Callout with StaticResourceCalloutMock

  • Test a Callout with HttpCalloutMock

In your code,you need to create fake response.I will just add a skelton for you.its already available in trailhead:

Please check below link:


creating Fake response

 global class AnimalsHttpCalloutMock implements HttpCalloutMock {
// Implement this interface method
global HTTPResponse respond(HTTPRequest request) {
    // Create a fake response
    HttpResponse response = new HttpResponse();
    response.setHeader('Content-Type', 'application/json');
    response.setBody('{"animals": ["majestic badger", "fluffy bunny", "scary 
 bear", "chicken", "mighty moose"]}');
    return response;
} }

Using fake response in below class

 @isTest static void testPostCallout() {
// Set mock callout class
Test.setMock(HttpCalloutMock.class, new AnimalsHttpCalloutMock());
// This causes a fake response to be sent
// from the class that implements HttpCalloutMock.
HttpResponse response = AnimalsCallouts.makePostCallout();
// Verify that the response received contains fake values
String contentType = response.getHeader('Content-Type');
System.assert(contentType == 'application/json');
String actualValue = response.getBody();
String expectedValue = '{"animals": ["majestic badger", "fluffy bunny", "scary bear", "chicken", "mighty moose"]}';
System.assertEquals(actualValue, expectedValue);
System.assertEquals(200, response.getStatusCode());}
  • Please check Test.setMock(HttpCalloutMock.class, new AnimalsHttpCalloutMock()); [class usage in above example].

  • In your code you commented that part and you have not created any fake response generating class


I believe you have plenty of other classes that calls to other classes which has a connection to your controller. Try adding your mock locator to the test class of your other test class. I had that error in my controller just recently. It turns out that my controller class calls my scheduler class which calls the class with a callout. So, I added the mock locator to my controller test class. Then it worked.

If you are wondering what is mock locator, this should be it

Test.setMock(HttpCalloutMock.class, new YourHttpCalloutMockImpl());

Not the answer you're looking for? Browse other questions tagged or ask your own question.