+ Start a Discussion
nxkhanh091.392265354033245E12nxkhanh091.392265354033245E12 

System.QueryException: List has no rows for assignment to SObject

I did try the query in Force.com Explore and get the result whoever when I ran the test this error comes up.  Please help!!!
Here is my code:

@istest
private class QuoteProductEntryTests {

    static testMethod void theTests(){
      
          
        QuoteLineItem qli = [select Id, PricebookEntryId, PriceBookEntry.Product2Id, QuoteId, Quote.OpportunityId from QuoteLineItem limit 1];
             
              
        ////////////////////////////////////////
        //  test QuoteProductEntry
        ////////////////////////////////////////
      
        // load the page     
        PageReference pageRef = Page.QuoteProductEntry;
        pageRef.getParameters().put('Id',qli.QuoteId);
        Test.setCurrentPageReference(pageRef);
      
        // load the extension
        QuoteitemExtension oPEE = new QuoteItemExtension(new ApexPages.StandardController(qli.Quote));
      
        // 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 = qli.PricebookEntryId;
        oPEE.removeFromShoppingCart();
        system.assert(oPEE.shoppingCart.size()==startCount-1);
      
        //test save and reload extension
        oPEE.onSave();
        oPEE = new QuoteItemExtension(new ApexPages.StandardController(qli.Quote));
        system.assert(oPEE.shoppingCart.size()==startCount-1);
      
        // test search again, this time we will find something
        oPEE.searchString = qli.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(QuoteLineItem o : oPEE.ShoppingCart){
            o.quantity = 5;
            o.unitprice = 300;
        }
        oPEE.onSave();
      
        // query line items to confirm that the save worked
        QuoteLineItem[] qli2 = [select Id from QuoteLineItem where QuoteId = :qli.QuoteId];
        system.assert(qli2.size()==startCount);
      
        // test on new Opp (no pricebook selected) to make sure redirect is happening
        Quote newQuote = new Quote(Name='New quote', OpportunityId=qli.Quote.OpportunityId);
        insert(newQuote);
        oPEE = new QuoteItemExtension(new ApexPages.StandardController(newQuote));
        System.assert(oPEE.priceBookCheck()!=null);
      
        // final quick check of cancel button
        System.assert(oPEE.onCancel()!=null);
      
      
        ////////////////////////////////////////
        //  test redirect page
        ////////////////////////////////////////
      
        // load the page

   
    }
}
Best Answer chosen by nxkhanh091.392265354033245E12
Adnubis LLCAdnubis LLC
When you are running tests the Test Enviroment does not have access to your existing data unless you tell it to. If you want your test to see your existing data use the following annotation.

@isTest(SeeAllData=true)

On a side note, it is not neccesarily the best practice to test using live data. Instead my recommendation is to create the data within the test so that you have full control. Live data can be deleted, changed, etc... which makes your testing unreliable.

All Answers

Adnubis LLCAdnubis LLC
When you are running tests the Test Enviroment does not have access to your existing data unless you tell it to. If you want your test to see your existing data use the following annotation.

@isTest(SeeAllData=true)

On a side note, it is not neccesarily the best practice to test using live data. Instead my recommendation is to create the data within the test so that you have full control. Live data can be deleted, changed, etc... which makes your testing unreliable.
This was selected as the best answer
nxkhanh091.392265354033245E12nxkhanh091.392265354033245E12
thank you very much Adnubis!!!!!! you saved my life literally!
ann.latham1.3946853206365056E12ann.latham1.3946853206365056E12
I was having the exact same problem with the same code.

Thank you nxkhanh for asking for help!! Thank you for Andubis for the answer!!!
cesarhankecesarhanke
Exactly what I needed!!