0

This question already has an answer here:

I am new to Apex coding and with lot of struggle I wrote the below apex class to find locations based on a Geocode custom object records. Now, I am having difficulties to write a testclass for the same, am still learning Code coverage skills but i'm really not able to do this quickly and that's the reason posting it here.

@RestResource(urlMapping='/locationservice')
global class LocationService 
{
    @HttpGet
    global static void doGet() 
    {
        //params from rest url
        string Zcode = RestContext.request.params.get('Zipcode');
        string ste  = RestContext.request.params.get('State');
        string city = RestContext.request.params.get('City');
        string mlrange = RestContext.request.params.get('milerange');
        string lat;
        string lon;
{
if(ste!=null)
    {
list<Geo_codes__c> lstPostalCode = [select Latitude__c, Longitude__c from Geo_codes__c where State__c= :ste];//soql to query Geocode Custom Object
        if(!lstPostalCodeMDT.isempty())
        {
           lat = lstPostalCode.get(0).Latitude__c;
           lon = lstPostalCode.get(0).Longitude__c;
        }
    }
if(city!=null)
    {
list<Geo_codes__c> lstPostalCode = [select Latitude__c, Longitude__c from Geo_codes__c where City__c = :city];//soql to query Geocode Custom Object
        if(!lstPostalCodeMDT.isempty())
        {
           lat =  lstPostalCode.get(0).Latitude__c;
           lon = lstPostalCode.get(0).Longitude__c;
        }
    }
if(Zcode!=null)
    {
        list<Geo_codes__c> lstPostalCode = [select Latitude__c, Longitude__c from Geo_codes__c where Zipcode__c = :Zcode];//soql to query Geocode Custom Object
        if(!lstPostalCodeMDT.isempty())
        {
           lat = lstPostalCode.get(0).Latitude__c;
           lon = lstPostalCode.get(0).Longitude__c;
        }
    }
}
decimal lt = decimal.valueOf(lat);
decimal lg = decimal.valueOf(lon);
decimal mlran = decimal.valueOf(mlrange);

//SOQL to fetch the dealer data
list<account> locationList = [SELECT Id, Name, Phone, ShippingStreet, ShippingCity, ShippingState, ShippingCountry, ShippingLatitude, ShippingLongitude, ShippingPostalCode, Website, distance(ShippingAddress, geolocation (:lt, :lg), 'mi') distance 
FROM Account WHERE distance(ShippingAddress, geolocation (:lt, :lg),'mi') < :mlran
ORDER BY distance(ShippingAddress, geolocation (:lt, :lg), 'mi') LIMIT 20];

//pass the results in JSON format     
String locationJSON = JSON.serialize(locationList);
RestContext.response.addHeader('Content-Type', 'application/json');
RestContext.response.responseBody = Blob.valueOf(locationJSON);    
    }
}

marked as duplicate by Reshma, Himanshu, Pranay Jaiswal, battery.cord, codeyinthecloud Jan 25 at 15:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.