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
Kyle BuzzaKyle Buzza 

Test Class for Account Geolocation Component

Hi, I'm using an apex class to connect to an external API, but the test class I wrote only covers 62%. How exactly would I write test classes to cover the http callouts in my code.

Here's the original code:
public class AccountGeocodeAddress {
	private static Boolean geocodingCalled = false;
	public static void DoAddressGeocode(id accountId) {
        if(geocodingCalled || System.isFuture()) {
    		System.debug(LoggingLevel.WARN,'***Address Geocoding Future Method Already Called - Aborting...');
    		return;
        }
		
        geocodingCalled = true;
		geocodeAddress(accountId);
	}
    

	@future (callout=true)
	static private void geocodeAddress(id accountId){ 
  		// Key for Google Maps Geocoding API
  		String geocodingKey = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
  		// get the passed in address
  		Account geoAccount = [SELECT BillingStreet, BillingCity, BillingState, BillingCountry, BillingPostalCode FROM Account WHERE id = :accountId];
    
  		// check that we have enough information to geocode the address
		if((geoAccount.BillingStreet == null) || (geoAccount.BillingCity == null)) {
    		System.debug(LoggingLevel.WARN, 'Insufficient Data to Geocode Address');
    		return;
  		}
  		// create a string for the address to pass to Google Geocoding API
  		String geoAddress = '';
        if(geoAccount.BillingStreet != null){
            geoAddress += geoAccount.BillingStreet + ', ';
        }
        if(geoAccount.BillingCity != null){
    		geoAddress += geoAccount.BillingCity + ', ';
        }
        if(geoAccount.BillingState != null){
    		geoAddress += geoAccount.BillingState + ', ';
        }
        if(geoAccount.BillingCountry != null){
    		geoAddress += geoAccount.BillingCountry + ', ';
        }
        if(geoAccount.BillingPostalCode != null){
    		geoAddress += geoAccount.BillingPostalCode;
        }
  
  		// encode the string so we can pass it as part of URL
  		geoAddress = EncodingUtil.urlEncode(geoAddress, 'UTF-8');
  		// build and make the callout to the Geocoding API
  		Http http = new Http();
  		HttpRequest request = new HttpRequest();
  		request.setEndpoint('https://maps.googleapis.com/maps/api/geocode/json?address=' + geoAddress + '&key=' + geocodingKey + '&sensor=false');
  		request.setMethod('GET');
  		request.setTimeout(60000);
  		try {
    		// make the http callout
    		HttpResponse response = http.send(request);
    		// parse JSON to extract co-ordinates
    		JSONParser responseParser = JSON.createParser(response.getBody());
    		// initialize co-ordinates
    		double latitude = null;
    		double longitude = null;
    		while(responseParser.nextToken() != null) {
      			if((responseParser.getCurrentToken() == JSONToken.FIELD_NAME) && (responseParser.getText() == 'location')) {
        			responseParser.nextToken();
        			while(responseParser.nextToken() != JSONToken.END_OBJECT) {         
						String locationText = responseParser.getText();
						responseParser.nextToken();
                        if (locationText == 'lat'){
           					latitude = responseParser.getDoubleValue();
                        } else if (locationText == 'lng'){
           					longitude = responseParser.getDoubleValue();
                        }
        			}
      			}
    		}
    		// update co-ordinates on address if we get them back
    		if(latitude != null) {
      			geoAccount.Location__Latitude__s = latitude;
      			geoAccount.Location__Longitude__s = longitude;
                System.debug('well we got here');
      			update geoAccount;
    		}
  		} catch (Exception e) {
    		System.debug(LoggingLevel.ERROR, 'Error Geocoding Address - ' + e.getMessage());
  		}
	}
}

and here's my current test class:
@isTest
public class geocodeAccountAddressTest {
    private static testmethod void geocodeTest1(){
        Account acc = new Account();
        acc.Name = 'testName';
        acc.Last_Name__c = 'LastName';
        acc.Email__c = 'acc@acc.com';
        acc.User_ID__c = 900;
        insert acc;
        
        acc.BillingStreet = '1111 Name Street';
        update acc;
        acc.BillingCity = 'Test City';
        acc.BillingState = 'PA';
        acc.BillingCountry = 'US';
        acc.BillingPostalCode = '15108';
        update acc;
    }
}

Thanks
Best Answer chosen by Kyle Buzza
Raj VakatiRaj Vakati
Refer this links .. You have code for the same 
https://github.com/dan24jones/Salesforce-Geocoding/blob/master/staticresources/GetGeocodeResourceUSA.resource

https://github.com/dan24jones/Salesforce-Geocoding/blob/master/classes/Geocode.cls

https://github.com/dan24jones/Salesforce-Geocoding/blob/master/classes/GeocodeTest.cls