-2

Controller:

global class GetcontentCalloutClass {

public GetcontentCalloutClass(ApexPages.StandardController controller) {}

public static Map<String, String> respStrings;
global static HttpResponse getcontentFromdftlyService(String rec_Id, String appkey) {
    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://example.com/example');
    req.setMethod('GET');

   // 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);
   System.debug(res.getBody());
   return res;
}

  @RemoteAction
  global static Map<String, String> get_content(String rec_Id, String appkey){
     HttpResponse res = GetcontentCalloutClass.getcontentFromdftlyService(rec_Id,appkey);
     respStrings = new Map<String, String>{'jsonString' => res.getBody()};
     return respStrings;
  }

}

Test Class:

@isTest

    public class get_content_links_test {
        static testMethod void testMethod1() 
        {
        dftly_Timesheet_Detail__c dTDetait = new dftly_Timesheet_Detail__c (Name = 'My Test Account');
        insert dTDetait ;
        Map<String, String> Test_MyStrings = get_content_links.get_content('Test body',dTDetait.id);
    }
}

Error message on run test: Methods defined as TestMethod do not support Web service.

1 Answer 1

3

Testing HTTP Callouts by Implementing the HttpCalloutMock Interface

global class ***YourHttpCalloutMockImpl*** implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest req) {
        // Create a fake response.
        // Set response values, and 
        // return response.
    }
}

Now as we have set the fake response we need to send this fake response by calling Test.setMock in test method

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

Below is the sample mock test class

@isTest
global class MockHttpResponseGenerator implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {
        // Optionally, only send a mock response for a specific endpoint
        // and method.
        System.assertEquals('http://example.com/example/test', req.getEndpoint());
        System.assertEquals('GET', req.getMethod());

        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"example":"test"}');
        res.setStatusCode(200);
        return res;
    }
}

Below is the sample test class

@isTest
private class CalloutClassTest {
     @isTest static void testCallout() {
        // Set mock callout class 
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());

        // Call method to test.
        // This causes a fake response to be sent
        // from the class that implements HttpCalloutMock. 
        HttpResponse res = CalloutClass.getInfoFromExternalService();

        // Verify response received contains fake values
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = res.getBody();
        String expectedValue = '{"example":"test"}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, res.getStatusCode());
    }
}

public class CalloutClass {
    public static HttpResponse getInfoFromExternalService() {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://example.com/example/test');
        req.setMethod('GET');
        Http h = new Http();
        HttpResponse res = h.send(req);
        return res;
    }
}

For more detail please see the standard doc

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .