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
SarisfdcSarisfdc 

How to write test class for @future method using Httprequest

global class webportal {
    
    @future(callout=true)
    public static void Importwebportal()
    {

        string webportalURL = 'https://api.testtt.com/testtesttestttttt=json';
        string webportalResponse = ''; 
        
  
        // Instantiate a new http object
        Http h = new Http();
    
        // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
        HttpRequest req = new HttpRequest();
        req.setEndpoint(webportalURL);
        req.setMethod('GET');
    
        // Send the request, and return a response
        try 
        {
           
            HttpResponse res = h.send(req);
            webportalResponse = res.getBody();
                  
        }
        catch (Exception ex)
        {
            System.debug('' + ex.getMessage()); 
        }
       
        webportalImport wb= new webportalImport ();
        wb.Import(webportalResponse);          
    
    }    
}

I have above code. I am not able to write test class for this..
Please tell me how to write test class for this. Iam not able to get the response from the HHTP in test class

Thanks
 
Jerome RussJerome Russ
Apex has an interface you can use to mock the HTTP callout: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_interface_httpcalloutmock.htm#apex_interface_httpcalloutmock

Here is an article on how to use it: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm
SarisfdcSarisfdc
Can you tell how to do in my scenario