1

Apex newbie here and would really appreciate the help on a test class I'm trying to write.

Basically, a VF page called "Invoice" is getting created when a button is pressed on the Quote object. The controller extension then does a query to pull certain fields from QuoteLineItems (to display on the VF page).

public class Quotes { 
     public Quotes(ApexPages.StandardController controller) {
     }

List<Quotelineitem> products;
public List<Quotelineitem> getProducts() {
     if(products == null) {
          products = [SELECT Id, Product2.Name, Quantity, UnitPrice, Subtotal, TotalPrice, Discount_Bath__c, Dispensator__c from Quotelineitem where QuoteId=:ApexPages.currentPage().getParameters().get('id')];
          }
     return products;
     }
}

Here's what I have so far for the test class, which is at least giving some partial coverage, but I'm at a loss as to what to do after successfully testing the creation of the controller.

@isTest
public class testInvoiceCreator {
static testMethod void testQuoteControllerCreate(){
    Quote a = new Quote(name='Test');
    ApexPages.StandardController sc = new ApexPages.StandardController(a);
    Quotes q = new Quotes(sc);
    system.assert(q != null);

 //what next?!
    }
}

Thank you all in advance for any and all help and apologies for asking for what most likely a very simple solution :(

1

1 Answer 1

1
  1. You need to give your controller some data to work with first. Which means that you will need to create a Quote, insert it, then create some QuoteLineItems and insert them. This should happen before you start testing.
  2. Since you are looking for the URL parameter 'id', you will need to add that parameter to your test context through the map ApexPages.CurrentPage().getParameters()
  3. After constructing an instance of the controller, you still need to explicitly call the method or methods you are testing. In this case, q.getProducts(); Make sure you capture the result into a list<QuoteLineItem>
  4. Go through the list of QuoteLineItems and make sure they are what you expect to get back. At least make sure there's the right number of records.

As Keith C shared in a comment, go through the docs, and they will say the same things. http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm

1
  • Gents, after toiling through this for the past few days this finally happened... Code Coverage 100% (6/6) I can't thank you both enough for your input, much appreciated!!!
    – TimD
    Oct 15, 2014 at 20:16

You must log in to answer this question.

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