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
BangoBango 

Simple Test Class Question

Hi there,

I wrote a a trigger to add a product to the opportunity, it works fine but I'm stuck at the test class which is giving me low code coverage, I need help with the test class. Please see the trigger/test class below, thanks in advance.

trigger AddOppLines on Opportunity (after update,after insert) 
{
    List<OpportunityLineItem> OppLineItems = new List<OpportunityLineItem>();
    for (Opportunity newOpp: Trigger.New) 
    {
            
            Integer C110013 = 1;
            
            for(PriceBookEntry a : [SELECT Id, ProductCode FROM PriceBookEntry WHERE PriceBook2.Name='Bulk TV 1'])
            {
                if (a.ProductCode == '110013' && C110013 > 0)
                {
                    OppLineItems.add(new OpportunityLineItem(OpportunityId = newOpp.Id,PricebookEntryId = a.Id,Quantity = C110013, UnitPrice = 0,Auto__c = TRUE));
                }
            }   
    }
    insert OppLineItems;
}


---------------------------------------------------------------------------------------------------------------------------------------------------------------


@isTest 
private class AddOppLinesTestClass 
{
    static testMethod void validateAddOppLines() 
    {
    
        PriceBook2 pb = new PriceBook2(Name='Bulk TV 1',IsActive=true);
        insert pb;
        
        Product2 p = new Product2(Name='TrigTest',IsActive=TRUE,ProductCode='110013',Description='TriggerTest');
        insert p;
        
        PricebookEntry pbe = new PricebookEntry(Product2Id = p.Id,PriceBook2Id = pb.Id,IsActive = true,UnitPrice = 20.00);
        
        Opportunity b = new Opportunity(Name='Test 1',StageName='New',CloseDate=Date.today());
        insert b;
    }
}
pconpcon
The problem is you never insert your pbe.  See line 13 below.
 
@isTest 
private class AddOppLinesTestClass 
{
    static testMethod void validateAddOppLines() 
    {
        PriceBook2 pb = new PriceBook2(Name='Bulk TV 1',IsActive=true);
        insert pb;
        
        Product2 p = new Product2(Name='TrigTest',IsActive=TRUE,ProductCode='110013',Description='TriggerTest');
        insert p;
        
        PricebookEntry pbe = new PricebookEntry(Product2Id = p.Id,PriceBook2Id = pb.Id,IsActive = true,UnitPrice = 20.00);
        insert pbe;
        
        Opportunity b = new Opportunity(Name='Test 1',StageName='New',CloseDate=Date.today());
        insert b;
    }
}

 
BangoBango
Thanks for the reply, I actually just fixed the issue, I changed @isTest to @isTest(SeeAllData=true) and it jumped to full coverage. I'm not sure what your answer meant, could you elaborate why you suggested not inserting the priceBookEntry? Take care.
BangoBango
Oh, I just noticed that I never inserted my pbe lol Sorry I got confused because you said "you never insert your pbe" not "you never inserted your pbe" so I thought you were suggesting that I shouldn't insert it :)
pconpcon
I beg you, please please please, do not user SeeAllData.  My guess is the reason that SeeAllData works is because you were not inserting your PricebookEntry.  If this is the case your tests will not work correctly if that data does not exist in Production.  There is almost no reason to legitimately use SeeAllData in your tests.
BangoBango
Hmm, interesting, I'm removing the SeeAllData and trying again
BangoBango
I got:

System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: []

 
pconpcon
What line is that occurring on?  Is it on line 13?