You need to sign in to do that
Don't have an account?
Travis Lee 6
Help improving test class code coverage
Hi All,
Trying to improve the code coverage of a test class I've written. The trigger is meant to unset a checkbox on the opportunity after the opportunity line items are updated, the checkbox is True and the line items have revenue schedules. Right now it's at 31% but I'm unsure how to replicate the necessary actions to push it beyond that? Any advice is appreciated! I'm trying to avoid becoming one of those developers whose code barely makes it to production. Here's the trigger I'm trying to cover and below that is the class that I've written so far.
Trigger:
Class:
Trying to improve the code coverage of a test class I've written. The trigger is meant to unset a checkbox on the opportunity after the opportunity line items are updated, the checkbox is True and the line items have revenue schedules. Right now it's at 31% but I'm unsure how to replicate the necessary actions to push it beyond that? Any advice is appreciated! I'm trying to avoid becoming one of those developers whose code barely makes it to production. Here's the trigger I'm trying to cover and below that is the class that I've written so far.
Trigger:
trigger unsetScheduler on OpportunityLineItem (after update) { Set<Id> setId = new Set<Id>(); for (OpportunityLineItem allLineItems : trigger.new) { if(allLineItems.HasRevenueSchedule == true) { setId.add(allLineItems.OpportunityId); } } if(setId.size() > 0 ) { Map<Id,Opportunity> mapOpp = new Map<Id,Opportunity>( [select id,Scheduling__c from Opportunity where id in :setId and Scheduling__c = true]); List<Opportunity> lstOppToUpdate = new List<Opportunity>(); for (OpportunityLineItem allLineItems: trigger.new) { if(allLineItems.HasRevenueSchedule == true && mapOpp.containsKey(allLineItems.OpportunityId) ) { Opportunity opp = mapOpp.get(allLineItems.OpportunityId); opp.Scheduling__c = false; lstOppToUpdate.add(opp); } } if(lstOppToUpdate.size()>0) { update lstOppToUpdate; } } }
Class:
@isTest public class testUnsetScheduler { static testMethod void updateLineItem() { Account acc = new Account(); acc.Name = 'Test Account'; insert acc; Opportunity opp = new Opportunity(); opp.Name = 'TestOpportunity'; opp.AccountId = acc.Id; opp.CloseDate = System.Today().addDays(10); opp.StageName = 'Negotiation'; opp.Projected_Start_Date__c = System.Today().addDays(15); opp.Projected_End_Date__c = System.Today().addDays(20); opp.Type = 'Media'; opp.Programmatic_TV_Rev__c = 'No'; opp.Scheduling__c = True; insert opp; OpportunityLineItem oli = new OpportunityLineItem(); oli.OpportunityId = opp.Id; oli.Budget__c = 10000; oli.New_Rate__c = 0.05; oli.UnitPrice = 0; oli.Quantity = 1; oli.PricebookEntryId = '01u7000000GB8I1'; insert oli; oli.Budget__c = 15000; oli.New_Rate__c = 0.10; update oli; } }
Try this and let me know if it works
All Answers
Try this and let me know if it works