You need to sign in to do that
Don't have an account?
Pranav_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; } } |
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.
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
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.
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.
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.
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!
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