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

code coverage help with json parser

I have created a class which helps to automatically update a geolocation field. I have also given the test class but the code coverage is only 72%. below is the code and test class. The lines of code which is in bold are not covered by the test class. Can anyone help with writing test class.

public class AccountGeocodeAddress{
// static variable to determine if geocoding has already occurred
private static Boolean geocodingCalled = false;
// wrapper method to prevent calling future methods from an existing future context
public static void DoAddressGeocode(id
accountId) {
(geocodingCalled || System.isFuture()) {
    System.debug(LoggingLevel.WARN, '***Address Geocoding Future Method Already Called - Aborting...');
  // if not being called from future context, geocode the address
  geocodingCalled = true;
// we need a future method to call Google Geocoding API from Salesforce
@future (callout=true)
static private void geocodeAddress(id accountId)

  // Key for Google Maps Geocoding API
  String geocodingKey = 'AIzaSyAYPyw3l--jV92bEfgq20vfAggygvqiIPg';
  // 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');
  //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('' + geoAddress + '&key=' + geocodingKey + '&sensor=false');
  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')) {
        while(responseParser.nextToken() != JSONToken.END_OBJECT) {
        String locationText = responseParser.getText();
        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;
      update geoAccount;

  } catch
(Exception e) {
    System.debug(LoggingLevel.ERROR, 'Error Geocoding Address - ' + e.getMessage());

Test Class

public class KioskGeocodeAddressTest {
    Static testMethod void testkioskGeocodeAdress(){
        FuseBox_Kiosk_Address__c kiosk = new FuseBox_Kiosk_Address__c(Name = 'Test');
        kiosk.Street_Name__c = '4500 Truxel rd';
        kiosk.Address_Line_2__c = 'Apt 1015';
        kiosk.City__c = 'Sacramento';
        Kiosk.State__c = 'California';
        Kiosk.Zip_Code__c = '95833';
        kiosk.Country__c = 'United States';
        insert kiosk;
        string status;
        HttpRequest request = new HttpRequest();
        StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
        mock.setHeader('Content-Type', 'application/json');
        HttpResponse response = new HttpResponse();
        JSONParser responseParser = JSON.createParser(response.getBody());
        String Loc = responseParser.getText();
        double latitude = 33.33333;
        double longitude;
        // Set the mock callout mode
        Test.setMock(HttpCalloutMock.class, mock);
        // Call the method that performs the callout
Hi Andy,

Did you get solution for the above test class.Am also facing same issue need to cover json part from as you mectioned.

If you already got any solution can you share it will helpful for me.

Thanks in advance.
Hi Andy,
I got the solution please check my updated code in set body am passing location like this way
if you are facing same issue please check my code.
res.setBody('{"name":"testrec","billingcountry":"United States","BillingState":"New Jersey","BillingCity":"California","BillingPostalcode":"07010","location":{'+'"lat" : 37.79410130,'+'"lon":-122.39510960'+'}}');