You need to sign in to do that
Don't have an account?
DaNae Peterson
Need a test class to cover a trigger AND class with a future method
Hi all! I am still fairly new to development and need help writing a test class for a class I have developed (my experience is with triggers). I am trying to autosync a quote to an opportunity upon creation and after studying some blog posts I came up with the following class:
public class QuoteAutoSyncUtil
{
@future
public static void syncQuote(Map<Id, Id> quoteMap)
{
List<Opportunity> oppList = new List<Opportunity>();
for(Id currentQuote : quoteMap.keyset())
{
Opportunity opp = new Opportunity();
opp.Id = quoteMap.get(currentQuote);
opp.SyncedQuoteId = currentQuote;
oppList.add(opp);
}
Integer oppSize = oppList.size();
update oppList[oppSize -1 ];
}
}
And the following trigger:
trigger QuoteAutoSync on Quote (after insert)
{
Map<Id, Id> quoteMap = new Map<Id, Id>();
for(Quote currentQuote : Trigger.New)
{
if(currentQuote.ExpirationDate != NULL)
{
quoteMap.put(currentQuote.Id, currentQuote.OpportunityId);
}
}
QuoteAutoSyncUtil.syncQuote(quoteMap);
}
It is working perfectly in Sandbox but I am getting stuck on the test class. This is what I have so far:
@isTest
public class AutoSyncQuote {
public static testmethod void TestAutoSyncQuote(){
Account a = new Account();
a.Name = 'Test Account';
a.salesReach__Agent_Account_Company_Name__c = '001a000001DbY9F';
a.salesReach__Agent_Contact_Name__c = '003a000001iM38p';
a.Account_Status__c = 'Active';
a.Diamond_Account__c = True;
a.Net_One__c = False;
Insert a;
Opportunity opp = new Opportunity();
opp.Name = 'Telecom Product or Service';
opp.CloseDate = Date.today();
opp.StageName = 'Quote Requested By Client';
opp.Opportunity_Type__c = 'Install New Service';
opp.AccountId = a.Id;
Insert opp;
Quote q = new Quote();
q.Name = 'Telecom Product or Service - Quote';
q.ExpirationDate = opp.Quote_Expiration_Date__c;
q.OpportunityId = opp.Id;
Map<Id, Id> quoteMap = new Map<Id, Id>();
QuoteAutoSyncUtil.syncQuote(quoteMap);
}
}
but so far it is saying none of my lines are covered. PLEASE HELP!!! Thank you very much!
public class QuoteAutoSyncUtil
{
@future
public static void syncQuote(Map<Id, Id> quoteMap)
{
List<Opportunity> oppList = new List<Opportunity>();
for(Id currentQuote : quoteMap.keyset())
{
Opportunity opp = new Opportunity();
opp.Id = quoteMap.get(currentQuote);
opp.SyncedQuoteId = currentQuote;
oppList.add(opp);
}
Integer oppSize = oppList.size();
update oppList[oppSize -1 ];
}
}
And the following trigger:
trigger QuoteAutoSync on Quote (after insert)
{
Map<Id, Id> quoteMap = new Map<Id, Id>();
for(Quote currentQuote : Trigger.New)
{
if(currentQuote.ExpirationDate != NULL)
{
quoteMap.put(currentQuote.Id, currentQuote.OpportunityId);
}
}
QuoteAutoSyncUtil.syncQuote(quoteMap);
}
It is working perfectly in Sandbox but I am getting stuck on the test class. This is what I have so far:
@isTest
public class AutoSyncQuote {
public static testmethod void TestAutoSyncQuote(){
Account a = new Account();
a.Name = 'Test Account';
a.salesReach__Agent_Account_Company_Name__c = '001a000001DbY9F';
a.salesReach__Agent_Contact_Name__c = '003a000001iM38p';
a.Account_Status__c = 'Active';
a.Diamond_Account__c = True;
a.Net_One__c = False;
Insert a;
Opportunity opp = new Opportunity();
opp.Name = 'Telecom Product or Service';
opp.CloseDate = Date.today();
opp.StageName = 'Quote Requested By Client';
opp.Opportunity_Type__c = 'Install New Service';
opp.AccountId = a.Id;
Insert opp;
Quote q = new Quote();
q.Name = 'Telecom Product or Service - Quote';
q.ExpirationDate = opp.Quote_Expiration_Date__c;
q.OpportunityId = opp.Id;
Map<Id, Id> quoteMap = new Map<Id, Id>();
QuoteAutoSyncUtil.syncQuote(quoteMap);
}
}
but so far it is saying none of my lines are covered. PLEASE HELP!!! Thank you very much!
"quoteMap" is set up in the trigger based on the ExpirationDate not being null.
The ExpirationDate is setup in test class by assigning the opportunity field Quote_Expiration_Date__c but that field is not set up in the test data. Unless there is other triggers/workflows in play then that field will remain null.
If, in the test data, you explicitly set the ExpirationDate then your code coverage will be 100%.
As I don't have the fields you have I've cut down the test class a little. The key difference is "q.ExpirationDate = Date.today();"
All Answers
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_invoking_future_methods.htm
You may use the following code as reference. BTW, in the future class, I don't understand why you use:
Integer oppSize = oppList.size();
update oppList[oppSize -1 ];
instead of:
update opplost;
"quoteMap" is set up in the trigger based on the ExpirationDate not being null.
The ExpirationDate is setup in test class by assigning the opportunity field Quote_Expiration_Date__c but that field is not set up in the test data. Unless there is other triggers/workflows in play then that field will remain null.
If, in the test data, you explicitly set the ExpirationDate then your code coverage will be 100%.
As I don't have the fields you have I've cut down the test class a little. The key difference is "q.ExpirationDate = Date.today();"