+ Start a Discussion
JN22JN22 

Test to Delete LineItems

Hello,

I have a trigger that queries LineItems on an Opportunity and updates the Opportunity with counts.  The test class below returns coverage of 54% and is not covering deletion of line items.  I have a delete statement on line 78 that I thought would work, but is there something else I need to do?

Test Class:
// This class tests the trigger named OppUpdates.

@isTest(seeAllData=true)
private class TestOppUpdates {

    static testMethod void TestOppUpdates() {       
                    
//Data Prep - Create Account, Opportunity, Product, etc.

        Account acct = new Account(name='Test Account', Type = 'Employer');
        insert acct;
        
    //Create Opportunity on Account
        Opportunity Opp = new Opportunity(Name='Test Account - New Opp1');
        Opp.StageName = 'Stage 1 - Learn';
        Opp.CloseDate = Date.today();
        Opp.AccountId = acct.id;
        insert Opp;         
                       
    // Create Deliverables 

    List<product2> Insertprodlist= new list<product2>();

        //High-Risk
        Product2 deliv1 = new Product2 (name='HR');
        deliv1.ProductCode = 'COACH_HR';
        deliv1.Product_Group__c = 'HR';
        Insertprodlist.add(deliv1);

        //Moderate-Risk
        Product2 deliv2 = new Product2 (name='MR');
        deliv2.productcode = 'COACH_MR';
        deliv2.Product_Group__c = 'MR';
        Insertprodlist.add(deliv2);

        
        insert Insertprodlist;
        
    // Get Pricebook
         Pricebook2 testpb = [select id from Pricebook2 where IsStandard = true];   
         
         List<PricebookEntry> InsertPricebookList= new List<PricebookEntry>();

// Add to pricebook
         PricebookEntry testdeliv1 = new PricebookEntry ();
         testdeliv1.pricebook2id = testpb.id;
         testdeliv1.product2id = deliv1.id;
         testdeliv1.IsActive = True;
         testdeliv1.UnitPrice = 10000;
         testdeliv1.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv1);
         
         PricebookEntry testdeliv2 = new PricebookEntry ();
         testdeliv2.pricebook2id = testpb.id;
         testdeliv2.product2id = deliv2.id;
         testdeliv2.IsActive = True;
         testdeliv2.UnitPrice = 10000;
         testdeliv2.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv2);
         
         Insert InsertPricebookList;
         

test.starttest();
        List<OpportunityLineItem> oli1 = new List<OpportunityLineItem>();
       // integer todo = 300;
       integer todo = 20;
        for(integer bi=0; bi<todo; bi++) {
            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv1.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv2.id, OpportunityId = Opp.id) );
         }
    
      insert oli1;  
   
      delete oli1; 
        
        test.stoptest();

    }
}


logontokartiklogontokartik
Hi JN22,

I dont see any assert statements in your test class and its kind of hard for anyone to say why its not working. 

Since you said you are updating counts ,

Can you add assert to make sure your counts are matching, like when you insert line items, query Opportunity to get the count field and assert with inserted line item size.

Also, please add assert after insert statement by querying the line items to make sure they are all inserted fine 

This should help us know if you are getting expected result. Also will let us know why delete doesnt work.

Thank you

JN22JN22
Thanks.  I've never used assert statements.  Would I just input someting like System.AssertNotEquals (null,oli1) at line 77?