0

I have a trigger on Account that makes a call to an apex class:

trigger AccountAddressTrigger on Account (after insert) {

Id patientAccountRecordTypeId = [SELECT Id, RecordType.DeveloperName FROM RecordType WHERE RecordType.DeveloperName = 'Patient_Account'].Id;

  for(account a:trigger.new){
    if( trigger.isInsert &&
        a.PersonMailingStreet != null &&
        a.PersonMailingCity != null &&
        a.RecordTypeId == patientAccountRecordTypeId &&
       personAccountsGeoCoder.isFirstTime){
           System.debug('isInsert');
        personAccountsGeoCoder.isFirstTime = false;
        personAccountsGeoCoder.updatePersonAccount(a.Id); //this is the apex class
    }
}

}

the Apex class : personAccountsGeoCoder then makes a callout using Salesforce's maps API. I go over the response and then grab the latitude and longitude values from the response and put them into the account record and do a DML update.

public class personAccountsGeoCoder {
    public static Boolean isFirstTime = true;
@future (callout=true)
public static void updatePersonAccount(Id accountToUpdate) { 
// Create and add an address.
Account a = [SELECT Id, PersonMailingStreet, PersonMailingCity,PersonMailingState, PersonMailingPostalCode, PersonMailingCountry FROM Account WHERE Id =: accountToUpdate];
string shippingAddress = a.PersonMailingStreet+ a.PersonMailingCity + a.PersonMailingState + a.PersonMailingPostalCode+ a.PersonMailingCountry;

Map<string,object> options = new Map<String,Object> {
            'version' => '1', // Required. Version of the API endpoint. Must be '1'.
            'address' => shippingAddress
        };

// Call the Geocode() method with the address.
Map<String, Object>response = maps.API.Geocode(options); //this is the web callout 

System.debug('::response::' + response);
// Log the resulting geographical coordinates and formatted address. 

Map<String, Object> response2 = (Map<String, Object>) response.get('data');
System.debug('::response2::' + response2);
Map<String, Object> response3 = (Map<String, Object>) response2.get('position');
System.debug('::response3::' + response3);
Decimal lat = (Decimal) response3.get('lat');
Decimal longi = (Decimal) response3.get('lng');

a.BillingLatitude = lat;
a.BillingLongitude = longi;
update a;
}

}

This works in Salesforce, but Im having issues writing the test class. This is what I have so far

@isTest
public class UpdateBillingCoordinatesTest {
@isTest
public static void service_call() {

    StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
    mock.setStaticResource('mapsGeoCodemockResponse');
    mock.setStatusCode(200);
    mock.setHeader('Content-Type', 'application/json');

    Test.setMock(HttpCalloutMock.class, mock);

    Id patientAccountRecordTypeId = [SELECT Id, RecordType.DeveloperName FROM RecordType WHERE RecordType.DeveloperName = 'Patient_Account'].Id;
    Account testAccount = new Account(
        RecordTypeId = patientAccountRecordTypeId,
        LastName = 'Test Account',
        PersonMailingStreet = '123 Test Street',
        PersonMailingCity = 'Indianapolis',
        PersonMailingState = 'IN',
        PersonMailingPostalCode = '46201',
        PersonMailingCountry = 'United States'
    );

    Test.startTest();
    insert testAccount;
    Test.StopTest();

    Account assertAcc = [SELECT Id, BillingLatitude, BillingLongitude FROM Account WHERE LastName = 'Test Account' LIMIT 1];
    System.debug('assertAcc' + assertAcc.BillingLatitude);
    System.assertEquals(37.78977, testAccount.BillingLatitude);
}       

}

I made a static resource via setup>static resources>new and just uploaded a file containing the Json as provided by Salesforce (Salesforce Json Example)

However I am still getting the error : 'Methods defined as TestMethod do not support Web service callouts'

Am I missing something here?

happyLion
  • 11
  • 1
  • 7

0 Answers0