function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
test vijaytest vijay 

Methods defined as TestMethod do not support Web service callouts + apex class

hi developers,
@future (callout=true)
    Public static void sendHTTPRequest(String url, String body){
        
        Http http = new Http();
        HttpResponse res = new HttpResponse();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setBody(body);
        req.setMethod('POST');
        
        try {
            res = http.send(req);
        } catch(System.CalloutException e) {
            System.debug('CalloutException: '+ e);
            System.debug(res.toString());
        }
 }
above is my future callout method. I am trying to cover this method but error is coming : Methods defined as TestMethod do not support Web service callouts. 
following is my test class.

@isTest
public class colloutTest {
@isTest
    public static void testAccTriHandler(){
        test.startTest();
       string key = 'https://orgBaseUrl.salesforce.com/identity/connect/token';
       string body = '{"name" : 'dummy'}';  
  AccountCustomerNumber.sendHTTPRequest(key, body);
        test.stopTest(); 
    }
  
}
can somebody guide me that how to fix this problam.
Thanks in advance
Best Answer chosen by test vijay
ShivankurShivankur (Salesforce Developers) 
Hi Vijay,

You should implement HttpCalloutMock class when testing with Web servoce callouts in Apex.

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

Please modify your test class in following manner:
@isTest 
private class Test_class {

    private class RestMock implements HttpCalloutMock {
        
        public HTTPResponse respond(HTTPRequest req) {
            String fullJson = 'your Json Response';
            
            HTTPResponse res = new HTTPResponse();
            res.setHeader('Content-Type', 'text/json');
            res.setBody(fullJson);
            res.setStatusCode(200);
            return res;
        }
    }
    static testMethod void service_call() {
        
        Test.setMock(HttpCalloutMock.class, new RestMock());
        Test.startTest();
        //your webserive call code
        Test.StopTest();
    }
}
Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.
 

All Answers

ShivankurShivankur (Salesforce Developers) 
Hi Vijay,

You should implement HttpCalloutMock class when testing with Web servoce callouts in Apex.

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

Please modify your test class in following manner:
@isTest 
private class Test_class {

    private class RestMock implements HttpCalloutMock {
        
        public HTTPResponse respond(HTTPRequest req) {
            String fullJson = 'your Json Response';
            
            HTTPResponse res = new HTTPResponse();
            res.setHeader('Content-Type', 'text/json');
            res.setBody(fullJson);
            res.setStatusCode(200);
            return res;
        }
    }
    static testMethod void service_call() {
        
        Test.setMock(HttpCalloutMock.class, new RestMock());
        Test.startTest();
        //your webserive call code
        Test.StopTest();
    }
}
Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.
 
This was selected as the best answer
test vijaytest vijay
thanks Shivankur ,
it's working.