+ Start a Discussion
NeelaNeela 

I'm new to apex and writing test classes. can someone help me in writing vf callout test class for this class with max code coverage?

The error in test class is "Illegal assignment from System.PageReference to System.HttpResponse".  PLEASE SOMEONE HELP WITH THIS IN GETTI NG MAX CODE COVERAGE.
====================================================
My controller class code:


public class vfClass {
    public String InputLabel{get;set;}
    public decimal GeoPosition{get;set;}
    public decimal GeoPosition2{get;set;}
    public PageReference makeCallout(){
        if(InputLabel==NULL || InputLabel=='' ){
            //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please Enter a Location'));
        }
        else{
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            String apikey = 'zrAR6DRKkoC4gRoku7QhnIiSjUxPNLMO';
            String endPoint = 'http://dataservice.accuweather.com/locations/v1/cities/search?q='+InputLabel+'&apikey='+apikey;
            if(Test.isRunningTest()) {
            endPoint = 'http://dataservice.accuweather.com/locations/v1/cities/search?q=Delhi&apikey=dummy';    
            }
            request.setEndpoint(endPoint);
            request.setMethod('GET');
            HttpResponse response = http.send(request);
            System.debug('response status-->'+response.getStatusCode());
            System.debug('response -->'+response);
            if (response.getStatusCode() == 200) {
                System.debug('body-->'+response.getBody());
                Object resp = JSON.deserializeUntyped(response.getBody());
                System.debug('resp-->'+resp);
                List<Object> theJsonList = (List<Object>) resp;
                System.debug('theJsonList-->'+theJsonList);
                List<Map<String, Object>> theJsonMapList = new List<Map<String, Object>>();
                
                for (Object obj : theJsonList) {
                    theJsonMapList.add((Map<String, Object>) obj);
                }
                System.debug('theJsonMapList-->'+theJsonMapList[0].get('GeoPosition'));
                Map<String,Object> GeoPositionMap = (Map<String,Object>)theJsonMapList[0].get('GeoPosition');
                System.debug('GeoPositionMap->'+GeoPositionMap.get('Latitude'));
               GeoPosition = (decimal)GeoPositionMap.get('Latitude');
               GeoPosition2 =(decimal)GeoPositionMap.get('Longitude');
                
            }
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Searching For GeoPositions'));
        }
        
        return null;
    }
}
=====================================================
Mock test code:
@isTest
global class vfnmock implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest request) {
         HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{GeoPosition": {"Latitude": 28.643,"Longitude": 77.118}}');
        response.setStatusCode(200);
        return response;
    }
}
====================================================
Test Class (that I tried):

@isTest
private class TestLocations {
     @isTest static void testCallout() {

        Test.setMock(HttpCalloutMock.class, new vfnmock());
        vfClass vfinst = new vfClass();
        HttpResponse res = vfinst.makeCallout();
        
        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());
    }
}

The error in test class is "Illegal assignment from System.PageReference to System.HttpResponse".  PLEASE SOMEONE HELP WITH THIS IN GETTI NG MAX CODE COVERAGE.
Best Answer chosen by Neela
Maharajan CMaharajan C
Hi Bhagya,

The error is due to the this line in test class :  HttpResponse res = vfinst.makeCallout();  . In Class you are returning the pagerefernce not the HttpResponse. 

There is a changes required in your mock class and Test Class:

Mockclass:
 
@isTest
global class vfnmock implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest request) {
         HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('[{"Version": 1,"Key": "206671","Type": "City","Rank": 11,"LocalizedName": "Chennai","EnglishName": "Chennai","PrimaryPostalCode": "","Country": {"ID": "IN","LocalizedName": "India","EnglishName": "India"},"GeoPosition": {"Latitude": 13.038,"Longitude": 80.245,"Elevation": {"Metric": {"Value": 17,"Unit": "m","UnitType": 5}}},"IsAlias": false}]');
        response.setStatusCode(200);
        return response;
    }
}

Test Class:
 
@isTest
public class TestLocations {
    @isTest static void testCallout() {
        
        Test.setMock(HttpCalloutMock.class, new vfnmock());
        vfClass vfinst = new vfClass();
        vfinst.InputLabel = 'Test';
        Test.startTest();
        vfinst.makeCallout();
        Test.stopTest();
        system.assertEquals(vfinst.GeoPosition ,13.038);
        system.assertEquals(vfinst.GeoPosition2 ,80.245);
    }
}


Thanks,
Maharajan.C