I'm trying to create a test class for a trigger that executes business logic based on the a custom Geolocation field on the quote (this calls a class that does and http callout to retrieve the street address via reverse-geolocation).
However, the Geolocation codes are always being read as null, even when they are populated in the test class.
Here is the test class:
@isTest
private class quoteTriggerTest {
@TestSetup static void createOpportunity(){
Opportunity opp = new Opportunity();
opp.Name = 'Test Opportunity';
opp.CloseDate = date.today().addDays(30);
opp.StageName = 'Qualification';
insert opp;
}
@isTest static void newQuoteWithGeolocation(){
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote with Geolocation';
q.Job_Site_Geolocation__latitude__s = 36.072528;
q.Job_Site_Geolocation__longitude__s = -95.925575;
insert q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
}
@isTest static void newQuoteWithoutGeolocation(){
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test
Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote without Geolocation';
insert q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
q.Name = 'Updated Test Quote without Geolocation';
update q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
}
@isTest static void updatedQuoteWithGeolocation(){
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test
Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote Updated with Geolocation';
insert q;
q.Job_Site_Geolocation__latitude__s = 36.072528;
q.Job_Site_Geolocation__longitude__s = -95.925575;
update q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
}
}
And here is the trigger:
trigger quoteTrigger on Quote (after insert, after update) {
List<Quote> quoteList = new List<Quote>();
if(Trigger.isInsert){
for(Quote q : Trigger.new){
if (q.Job_Site_Geolocation__c != NULL) {
quoteList.add(q);
}
}
}
if(Trigger.isUpdate){
for(Quote q : Trigger.new){
Quote oldQuote = Trigger.oldMap.get(q.Id);
Quote newQuote = Trigger.newMap.get(q.Id);
if(oldQuote.Job_Site_Geolocation__c !=
newQuote.Job_Site_Geolocation__c){
quoteList.add(q);
}
}
}
for(Quote q : quoteList){
System.debug('Quote Id: ');
}
retrieveAddress.getAddress(quoteList);
}
I can see from the debug log that the variables in the test class do have the geolocation codes, but when the the DML gets executed and the trigger fires, the geolocation field is null.
When I manually create a Quote with the geolocation data in the database, the trigger also fails to see that the geolocation field is not null.
Is there something additional I need to do in the trigger so that it can see the geolocation field?
retrieveAddress.getAddress
and are you using Future or queuable?updatedQuoteWithGeolocation
after DML update ,Job_Site_Geolocation__c
is still blank?q = [SELECT Id , Job_Site_Geolocation__c FROM Quote WHERE Id=:q.id]; System.debug(q.Job_Site_Geolocation__c ;