+ Start a Discussion

How to write test class for @future method using Httprequest

global class webportal {
    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();
        // Send the request, and return a response
            HttpResponse res = h.send(req);
            webportalResponse = res.getBody();
        catch (Exception ex)
            System.debug('' + ex.getMessage()); 
        webportalImport wb= new webportalImport ();

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

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
Can you tell how to do in my scenario