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
Pranav_VaidyaPranav_Vaidya 

Apex trigger test class fails

Hi,

 

Below is my trigger code. The trigger itself works as expected however when I test it using a test class the test coverage shown is only 64% and this less % does not allow me to include the custom object to my package.

 

I have highlighted the lines of code which are not covered by test run. How do I make the test coverage more than 75%.

Any help is much is appreciated. Thanks.

 

trigger TripAfterTrigger on Trip__c (after insert,after update) {
  
//Update Travel Budget table forecasts
   Trip__c CurrTrip = (Trip__c) Trigger.new[0]; 
   string BudMonth='';
   
   if(CurrTrip.Month__c != null && CurrTrip.Total_Flight_cost__c!=null){
     BudMonth=CurrTrip.Month__c;
    
     List <TravelBudgetDetail__c> tbc = [select Travel_Budget_Forecast_Month__c, Travel_Month__c from TravelBudgetDetail__c
                                         where  Travel_budget__r.Asset_class_Travel_Budget__c = :CurrTrip.Analyst_Asset_Class__c];
     for (integer i=0; i<tbc.size();i++) {
      if(tbc[i].Travel_Month__c!=null){ 
        if(tbc[i].Travel_Month__c.substring(0,3).equalsIgnoreCase(BudMonth)){
          if (tbc[i].Travel_Budget_Forecast_Month__c == null) {
            tbc[i].Travel_Budget_Forecast_Month__c = 0;
          }
          tbc[i].Travel_Budget_Forecast_Month__c = tbc[i].Travel_Budget_Forecast_Month__c + CurrTrip.Total_Flight_cost__c; 
          break;
        }  
       }
     }   
     Update tbc;
   }

 }

 

Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

Hi, since you have not provided any code for your test coverage. Since it is not covering the code written inside an if condition, I guess that condition is not getting satisfied in your test data. 

// add any other fields if required for your objects.
 
 Analyst_Asset_Class__c asset = new Analyst_Asset_Class__c();
 asset.Name = 'testAsset';
 insert asset; 

 Travel_budget__c = tb = new Travel_budget__r();
 tb.Name = 'test travel budget';
 tb.Asset_class_Travel_Budget__c = asset.Id;
 insert tb;
 
 
 TravelBudgetDetail__c tbd = new TravelBudgetDetail__c();
 tbd.Name = 'budget1';
 tbd.Travel_Budget_Forecast_Month__c = 2;
 tbd.Travel_Month__c = 3;
 tbd.Travel_budget__r = tb.Id;
 insert tbd;

 
 Trip__c trip1 = new Trip__c();
 trip1.Name = 'trip1';
 trip1.Month = date.today().month();
 trip1.Total_Flight_cost__c = 15000;
 trip1.Analyst_Asset_Class__c = asset.Id;
 insert trip;

 The field in RED was not having any value, hence your code was not covered written inside it. Try the above code in your test coverage class and let me know if it works.

All Answers

vishal@forcevishal@force

Hi, since you have not provided any code for your test coverage. Since it is not covering the code written inside an if condition, I guess that condition is not getting satisfied in your test data. 

// add any other fields if required for your objects.
 
 Analyst_Asset_Class__c asset = new Analyst_Asset_Class__c();
 asset.Name = 'testAsset';
 insert asset; 

 Travel_budget__c = tb = new Travel_budget__r();
 tb.Name = 'test travel budget';
 tb.Asset_class_Travel_Budget__c = asset.Id;
 insert tb;
 
 
 TravelBudgetDetail__c tbd = new TravelBudgetDetail__c();
 tbd.Name = 'budget1';
 tbd.Travel_Budget_Forecast_Month__c = 2;
 tbd.Travel_Month__c = 3;
 tbd.Travel_budget__r = tb.Id;
 insert tbd;

 
 Trip__c trip1 = new Trip__c();
 trip1.Name = 'trip1';
 trip1.Month = date.today().month();
 trip1.Total_Flight_cost__c = 15000;
 trip1.Analyst_Asset_Class__c = asset.Id;
 insert trip;

 The field in RED was not having any value, hence your code was not covered written inside it. Try the above code in your test coverage class and let me know if it works.

This was selected as the best answer
Pranav_VaidyaPranav_Vaidya

Hi,

 

Thanks for a quick reply. It has increased the test % to 71%. Thanks for your suggestion.

 

However, it was stil less than what it should be. Hence I removed the if block from my trigger and it is now 83%.

 

Thanks.

vishal@forcevishal@force

Well that might be because we have given a value here :

 

 tbd.Travel_Budget_Forecast_Month__c = 2;

 

and in the code you are checking when this field == null,  so remove the above line and try with the same code, it should definitely work!

vishal@forcevishal@force

I missed out on one more condition : 

 

if(tbc[i].Travel_Month__c.substring(0,3).equalsIgnoreCase(BudMonth))

 

Try this and let me know if this doesn't cover the code

 

// add any other fields if required for your objects.
 
 Analyst_Asset_Class__c asset = new Analyst_Asset_Class__c();
 asset.Name = 'testAsset';
 insert asset; 

 Travel_budget__c = tb = new Travel_budget__r();
 tb.Name = 'test travel budget';
 tb.Asset_class_Travel_Budget__c = asset.Id;
 insert tb;
 
 
 TravelBudgetDetail__c tbd = new TravelBudgetDetail__c();
 tbd.Name = 'budget1';
 tbd.Travel_Month__c = 'AUGUST';
 tbd.Travel_budget__r = tb.Id;
 insert tbd;

 
 Trip__c trip1 = new Trip__c();
 trip1.Name = 'trip1';
 trip1.Month__c = 'AUG';
 trip1.Total_Flight_cost__c = 15000;
 trip1.Analyst_Asset_Class__c = asset.Id;
 insert trip;