0

I've looked at the other answers for this question, but none of them really helped me understand how to fix it so sorry if this isn't really that different from the other questions, but I made a test class for an extension controller and it gets covered with my test class but theres also a failure (don't really know how but Im not gonna worry about it), but anyways my failure is System.QueryException: List has no rows for assignment to SObject. Heres my test class as of right now.

public class tryExtendedTestClass{
private final Quote quote;
public testMethod static void testController(){
    Quote q = new Quote(name='testQuote');
    List<QuoteLineItem> item = new List<QuoteLineItem>();
    item = [select id from QuoteLineItem where       id=:ApexPages.CurrentPage().getParameters().get('id')];

    Test.setCurrentPage(Page.PDFQuoteTemplate);
    ApexPages.CurrentPage().getParameters().put('id', q.id);

    ApexPages.StandardController sc = new ApexPages.StandardController(q);
    Controller_tryExtended ctl = new Controller_tryExtended(sc);

    System.assertEquals(ctl.pid, q.id);
}

Any Idea as to what to do to fix this? I've been working on this test method for the past few days and I just can't figure out how to get it to work.

2
  • You're querying on QuoteLineItem but you haven't inserted any QuoteLineItems to query.
    – Folkert
    Jun 17, 2015 at 14:55
  • @Folkert - good catch, but that is not the issue of the error as item is a list. OP needs to insert the quote after creating it as well as fix and deal with the null possibility in the other class
    – Eric
    Jun 17, 2015 at 14:58

1 Answer 1

0

It is not a problem with your test method, it is coming from code elsewhere:

Look at the error it will tell you the Classname and Line Number (Debug log).

this errors when you are trying to assign the results from a query (expecting only one result) to an sObject and there are no results:

Account a = [Select ID From Account Where id = :'001d0000001Wqer'];

If that ID DOES NOT exist the error will be thrown because the query returns a sObject[] and not a single object.

The way to fix it is to return the results to a list and check the results:

Account[] a = [Select ID From Account Where id = :'001d0000001Wqer'];
system.assertEquals(false,a.isEmpty(),'No Account found');

Per you comment the line is occurring in another class:

myQuote = [SELECT id, ISO4217_Currency__c, ContactId, ContractId, QuoteNumber FROM Quote WHERE id=:ApexPages.currentPage().getParameters().get('id')];

Because of this in your test class:

 ApexPages.CurrentPage().getParameters().put('id', q.id);

You never insert the Quote so q.id is null

6
  • It says my error is coming from here 'myQuote = [SELECT id, ISO4217_Currency__c, ContactId, ContractId, QuoteNumber FROM Quote WHERE id=:ApexPages.currentPage().getParameters().get('id')];'
    – Mike Stomp
    Jun 17, 2015 at 14:54
  • I do not see that in you code, but find that and either check for the urlParam of id to be null and not do the query or update as I suggested. I think you need to do as I suggested and write logic around what to do if the id is null as I expect it is null and not an incorrect value. See updated answer
    – Eric
    Jun 17, 2015 at 14:56
  • would i need a specific id in the controller class?
    – Mike Stomp
    Jun 17, 2015 at 14:58
  • when i try to insert Quote it says im missing a required field. the field is Opportunity.id
    – Mike Stomp
    Jun 17, 2015 at 15:13
  • Pretty self explanatory...
    – Eric
    Jun 17, 2015 at 15:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .