You need to sign in to do that
Don't have an account?
SolidLucas
test Class Help
Someone could help me with my class test for this?
public class CallOutMaps { public class Geocode{ public double lat {get; set;} public double lon {get; set;} } private static Geocode geocodificar(String address) { // construtor callout Geocode g = new Geocode(); Http h = new Http(); HttpRequest req = new HttpRequest(); //System.Debug ('http://maps.googleapis.com/maps/api/geocode/json?address='+address+'&sensor=false'); req.setEndpoint('http://maps.googleapis.com/maps/api/geocode/json?address='+address+'&sensor=false'); req.setMethod('GET'); req.setTimeout(60000); try{ // callout HttpResponse res = h.send(req); // Analisa as coordenadas de resposta //System.debug(res.getBody()); JSONParser parser = JSON.createParser(res.getBody()); double lat = null; double lon = null; while (parser.nextToken() != null) { if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'location')){ parser.nextToken(); // objeto início while (parser.nextToken() != JSONToken.END_OBJECT){ String txt = parser.getText(); parser.nextToken(); if (txt == 'lat'){ lat = parser.getDoubleValue(); //g.lat = parser.getDoubleValue(); } else if (txt == 'lng') lon = parser.getDoubleValue(); } } } //System.debug(lat + ' '+lon); g.lat = lat; g.lon = lon; System.debug(g); } catch (Exception ex) { } return g; } @future (callout=true) // Método futuro necessário para executar chamadas da trigger public static void geocodificarAccount(Id accountId){ /* TipoEndereco: 0 = Billing 1 = Shipping */ // Reuni informações da conta Account a = [SELECT Id,BillingCity,BillingCountry,BillingPostalCode,BillingState,BillingStreet ,BillingLatitude,BillingLongitude,ShippingState, ShippingStreet,ShippingPostalCode, ShippingLongitude, ShippingLatitude, ShippingCountryCode, ShippingCountry, ShippingCity,Futuro__c FROM Account WHERE id =: accountId]; // cria um endereço string String address = ''; if (a.BillingStreet != null) address += a.BillingStreet +', '; if (a.BillingCity != null) address += a.BillingCity +', '; if (a.BillingState != null) address += a.BillingState+', '; if (a.BillingPostalCode != null) address += a.BillingPostalCode +', '; if (a.BillingCountry != null) address += a.BillingCountry +', '; address = EncodingUtil.urlEncode(address, 'UTF-8'); String endereco = ''; if (a.ShippingStreet != null) endereco += a.ShippingStreet +', '; if (a.ShippingCity != null) endereco += a.ShippingCity +', '; if (a.ShippingState != null) endereco += a.ShippingState+', '; if (a.ShippingPostalCode != null) endereco += a.ShippingPostalCode +', '; if (a.ShippingCountry != null) endereco += a.ShippingCountry +', '; endereco = EncodingUtil.urlEncode(endereco, 'UTF-8'); Geocode g = geocodificar(address); Geocode e = geocodificar(endereco); System.debug('Passou!'+a.Id); // Atualiza as coordenadas caso voltarmos a.BillingLatitude = g.lat; a.BillingLongitude = g.lon; a.Futuro__c = true; a.ShippingLatitude = e.lat; a.ShippingLongitude = e.lon; update a; } }
my test class is that so far but gives me a error system.dmlException
@isTest private class tst_CallOutMaps { static testMethod void myUnitTest() { Account acc = new Account(); acc.Name = 'nome'; acc.ShippingStreet = 'Av das Nacoes unidas'; acc.ShippingCity = 'Cubatao'; acc.ShippingState = 'sp'; insert acc; //Test.startTest(); //SingleRequestMock fakeResponse = new SingleRequestMock(200,'Complete','[{"location" : {"lat" : 37.42291810, "lng" : -122.08542120}}]',null); //Test.setMock(HttpCalloutMock.class, fakeResponse); //LocationCallouts.getLocation(tmo.id); //System.assertEquals(/*check for expected results here...*/); } }
Abhinav Gupta
All you need is this official doc reading, it explains nicely how to use MockCallouts to test webservice integrations : https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http_testing_httpcalloutmock.htm