+ Start a Discussion
ann.latham1.3946853206365056E12ann.latham1.3946853206365056E12 

System.Assert Exception on Test Code previously deployed into production

I wrote and deployed a Test for using a VF page for product line entry.  My Test class worked previously.  Now I am getting a System.Assert Exception error for virtually the same code.  I only added additional required fields for the Opportunity Line Item.  Can anyone help?

Here is the test Class:

@istest (SeeAllData=true)
private class opportunityProductEntryTests {

    static testMethod void theTests(){
       
        // You really should create test data, but I'm going to query instead
        // It's my best shot of avoiding a test failure in most orgs
        // Once you've installed this package though, you might want to write your own tests
        // or at least customize these ones to make them more applicable to your org
           
        OpportunityLineItem oli = [select Id, PricebookEntryId, PricebookEntry.Pricebook2Id, PricebookEntry.Name, PriceBookEntry.Product2Id, OpportunityId, Opportunity.AccountId from OpportunityLineItem limit 1];
              
               
        ////////////////////////////////////////
        //  test opportunityProductEntry
        ////////////////////////////////////////
       
        // load the page      
        PageReference pageRef = Page.opportunityProductEntry;
        pageRef.getParameters().put('Id',oli.OpportunityId);
        system.Test.setCurrentPageReference(pageRef);
       
        // load the extension
        opportunityProductEntryExtension oPEE = new opportunityProductEntryExtension(new ApexPages.StandardController(oli.Opportunity));
       
        // test 'getChosenCurrency' method
        if(UserInfo.isMultiCurrencyOrganization())
            System.assert(oPEE.getChosenCurrency()!='');
        else
            System.assertEquals(oPEE.getChosenCurrency(),'');

        // we know that there is at least one line item, so we confirm
        Integer startCount = oPEE.ShoppingCart.size();
        system.assert(startCount>0);

        //test search functionality without finding anything
        oPEE.searchString = 'michaelforce is a hip cat';
        oPEE.updateAvailableList();
        system.assert(oPEE.AvailableProducts.size()==0);
       
        //test remove from shopping cart
        oPEE.toUnselect = oli.PricebookEntryId;
        oPEE.removeFromShoppingCart();
        system.assert(oPEE.shoppingCart.size()==startCount-1);
       
        //test save and reload extension
        oPEE.onSave();
        oPEE = new opportunityProductEntryExtension(new ApexPages.StandardController(oli.Opportunity));
        system.assert(oPEE.shoppingCart.size()==startCount-1);
       
        // test search again, this time we will find something
        oPEE.searchString = oli.PricebookEntry.Name;
        oPEE.updateAvailableList();
        system.assert(oPEE.AvailableProducts.size()>0);      

        // test add to Shopping Cart function
        oPEE.toSelect = oPEE.AvailableProducts[0].Id;
        oPEE.addToShoppingCart();
        system.assert(oPEE.shoppingCart.size()==startCount);
               
        // test save method - WITHOUT quanitities and amounts entered and confirm that error message is displayed
        oPEE.onSave();
        system.assert(ApexPages.getMessages().size()>0);
       
        // add required info and try save again
        for(OpportunityLineItem o : oPEE.ShoppingCart){
            o.quantity = 5;
            o.unitprice = 300;
            o.Term_Months__c = 12;
            o.Term_on_Product__c = FALSE;
            o.Opportunity_Type__c = 'New Business';
        }
        oPEE.onSave();
       
        // query line items to confirm that the save worked **HERE IS WHERE THE ERROR OCCURS**
        opportunityLineItem[] oli2 = [select Id from opportunityLineItem where OpportunityId = :oli.OpportunityId];
        system.assert(oli2.size()==startCount);
       
        // test on new Opp (no pricebook selected) to make sure redirect is happening
        Opportunity newOpp = new Opportunity(Name='New Opp',stageName='Pipeline',Amount=10,closeDate=System.Today()+30,AccountId=oli.Opportunity.AccountId);
        insert(newOpp);
        oPEE = new opportunityProductEntryExtension(new ApexPages.StandardController(newOpp));
        System.assert(oPEE.priceBookCheck()!=null);
       
        // final quick check of cancel button
        System.assert(oPEE.onCancel()!=null);
       
       
        ////////////////////////////////////////
        //  test redirect page
        ////////////////////////////////////////
       
        // load the page
        pageRef = Page.opportunityProductRedirect;
        pageRef.getParameters().put('Id',oli2[0].Id);
        system.Test.setCurrentPageReference(pageRef);

        // load the extension and confirm that redirect function returns something
        opportunityProductRedirectExtension oPRE = new opportunityProductRedirectExtension(new ApexPages.StandardController(oli2[0]));
        System.assert(oPRE.redirect()!=null);
    
    }

}
Bradley DelauneBradley Delaune
Hi!

Could you provide the line number or some more information about where the error is occuring?  Also, querying for an OpportunityLineItem to use in your test isn't really a best practice.  You should remove the (seeAllData=true) and create an Account, Opportunity, and some line items.  This will ensure that no matter which environment you deploy into, there will always be data to test with.
ann.latham1.3946853206365056E12ann.latham1.3946853206365056E12
Thank you Bradley.  I know that this Test Class isn't ideal.  I wanted to deploy and then continue to improve the code. I am perplexed on why it worked before and now it fails.  The same error occurs when "(seeAllData=true)" was removed.

Here's the error message:

Class opportunityProductEntryTests
Method Name theTests
Pass/Fail Fail
Error Message System.AssertException: Assertion Failed
Stack Trace Class.opportunityProductEntryTests.theTests: line 77, column 1
ann.latham1.3946853206365056E12ann.latham1.3946853206365056E12
The original code came from Michaelforce: http://www.michaelforce.org/recipeView?id=a0G30000006eVxVEAU
ann.latham1.3946853206365056E12ann.latham1.3946853206365056E12
I should also mention that the VF page and related APEX classes provides an easier way to enter Opportunity Line Items.  Where the test code is failing is testing whether you can save the line item to the opportunity after entering the required fields.  In this test, the opportunity exists, but you are adding the line item(s) on the VF page to test if you can successfully save the line items.  Maybe I need to create the Account and Opportunity first, and then add the required line item fields to test the Save function. I tried that approach in another version, and I am having trouble quering the data that I created.
Bradley DelauneBradley Delaune
Hello again.

Its likely that the Opportunity Line Item you are selecting here:
OpportunityLineItem oli = [select Id, PricebookEntryId, PricebookEntry.Pricebook2Id, PricebookEntry.Name, PriceBookEntry.Product2Id, OpportunityId, Opportunity.AccountId from OpportunityLineItem limit 1];
is linked to an opportunity that already has another line item.  When you get down to 
opportunityLineItem[] oli2 = [select Id from opportunityLineItem where OpportunityId = :oli.OpportunityId];
system.assert(oli2.size()==startCount);
You are potentially selecting Line Items that you just saved and ones that are already in the system.  This is why I recommended removing
(seeAllData=true)
and instead creating all of your data from scratch at the beginning of the test.
Nick KeehanNick Keehan
Hey Ann.

Did you end up getting this working? having the same issue as you around the sytem assert. 

Looking for help on the apex test as quite new ot apex.

Nick