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
Travis Lee 6Travis 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:
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;
       
    } 
}

 
Best Answer chosen by Travis Lee 6
swati_sehrawatswati_sehrawat
@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;
        
        Product2 objProd = new Product2();
        objProd.Name = 'Test';
        objProd.ProductCode = 'Test';
        objProd.IsActive = true;
        objProd.CanUseRevenueSchedule=true;
        insert objProd; 
        
        Id pricebookId = Test.getStandardPricebookId();
        PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = objProd.Id, UnitPrice = 10000, IsActive = true);
        insert standardPrice;
                  
        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 = standardPrice.id;
        
        insert oli;
        
        opportunityLineItemSchedule ols = new opportunityLineItemSchedule();
        ols.ScheduleDate = date.today();
        ols.opportunityLineItemID = oli.id;
        ols.type = 'revenue';
        ols.revenue= 2;
        insert ols;
        
        oli.UnitPrice = 1;
        update oli;
       
    } 
}

Try this and let me know if it works

All Answers

swati_sehrawatswati_sehrawat
@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;
        
        Product2 objProd = new Product2();
        objProd.Name = 'Test';
        objProd.ProductCode = 'Test';
        objProd.IsActive = true;
        objProd.CanUseRevenueSchedule=true;
        insert objProd; 
        
        Id pricebookId = Test.getStandardPricebookId();
        PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = objProd.Id, UnitPrice = 10000, IsActive = true);
        insert standardPrice;
                  
        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 = standardPrice.id;
        
        insert oli;
        
        opportunityLineItemSchedule ols = new opportunityLineItemSchedule();
        ols.ScheduleDate = date.today();
        ols.opportunityLineItemID = oli.id;
        ols.type = 'revenue';
        ols.revenue= 2;
        insert ols;
        
        oli.UnitPrice = 1;
        update oli;
       
    } 
}

Try this and let me know if it works
This was selected as the best answer
Travis Lee 6Travis Lee 6
It worked perfectly! 100% coverage, thank you so much. So was the real issue that I just didn't test far enough down the chain of related objects? It looks as though the biggest changes were the Line Item and Line Item Schedule chunks.
swati_sehrawatswati_sehrawat
Yes, in your trigger you have used "HasRevenueSchedule " so you need to make that true in test class also and for doing that you need to have schedules with type "Revenue".