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
Ryan MeyerRyan Meyer 

Unable to unit test callout from controller even though I set mock

Hi,

I have a controller for a page which does a http callout with the visitor's IP address to show their location. Page works great but I can't seem to get testing to work. I have a mock callout class that works for other classes but when I try to use it on this I receive the error "Methods defined as TestMethod do not support Web service callouts" -- is this because it's a controller and not a class?  Any help would be appreciated here.. banging my head against the table!

Relevant controlller excerpt:
 
public class CC_Auth_Class{
   public CC_Auth_Class(ApexPages.StandardController controller) {
    String strCity;
        String strRegion;
        String strCountry;
        Http httpProtocol = new Http();
        HttpRequest request = new HttpRequest();
        String endpoint = 'http://ipinfo.io/'+ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP')+'/json';
        request.setEndPoint(endpoint);
        request.setMethod('GET');
        HttpResponse response = httpProtocol.send(request);
        JSONParser parser = JSON.createParser(response.getBody());
        while (parser.nextToken() != null) {
            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'city')) {
                parser.nextToken();
                strCity = parser.getText();
            }
            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'region')) {
                parser.nextToken();
                strRegion = parser.getText();
            }
            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'country')) {
                parser.nextToken();
                strCountry = parser.getText();
            }
            }
        card.AUTH_LOCATION__c = strCity+', '+strregion+', '+strcountry;
    }
}



relevant test... where O = dummy opp 
ApexPages.StandardController sc = new ApexPages.StandardController(O);
        CC_Auth_Class AuthClass = new CC_Auth_Class(sc);
        PageReference pageRef = Page.CC_Auth_Form;
        pageRef.getParameters().put('id', String.valueOf(O.Id));
        Test.setMock(HttpCalloutMock.class, new FakeHTTPPost());
        Test.setCurrentPage(pageRef);

       
Ryan MeyerRyan Meyer
We are using FakeHTTPPost successfully for other classes.
Raj VakatiRaj Vakati
Can you please give me which lines are not covering? I guess you are not setting correct response as part of the mock 
Ryan MeyerRyan Meyer
It actually doesn't cover the JSONParser line or anything after it. I even changed our fake response to res.setBody('{"city":"test", "region":"test","country":"test"}'); without luck. It also fails to bother testing anything in the code after the error message "Methods defined as TestMethod do not support Web service callouts" which was covered code prior to this change.