QuoteLineItem tempQLI = [SELECT QuoteID FROM QuoteLineItem WHERE Discountable__c='YES' Limit 1];        
        String quoteId = tempQLI.quoteId;    
        Quote newtestQuote = [SELECT  ID
                              FROM Quote WHERE ID =:quoteId LIMIT 1];    
        List <QuoteLineItem> insertTestQuote = new List<QuoteLineItem>();        
        ApexPages.StandardSetController stdController = new ApexPages.StandardSetController(insertTestQuote);
        system.debug('stdController is: '+stdController);
        Controller_Quote_Discount Quote_Discount_Test = new Controller_Quote_Discount(stdController);

I'm creating a test class and I need to test a Quote. I do this by querying a Quotelineitem, grabbing it's QuoteId and then query a Quote with the quotelineitem's QuoteId. I then put the Quote into a list which I pass as an argument using StandardSetController to my Controller class.

However, in my Controller class, it's failing to grab the QuoteID as an argument the way it should. Here's my relevant Controller class code:

 public Controller_Quote_Discount(ApexPages.StandardSetController stdSetController)
        system.debug('quoteID in controller is: '+quoteID);

When I check to see what quoteId is in my public Controller class, it returns null. What is causing this behaviour?


To test Apex Controllers that use ApexPages.currentPage().getParameters method. Please refer to this topic Testing Custom Controllers and Controller Extensions. Constructing a StandardController or StandardSetController is not enough, you need to use Test.setCurrentPage in your test code.

// Setup mock current page with parameter in test context
String testId = 'a0KG000000PkPdW';
PageReference testPage = Page.myPage;
testPage.getParameters().put('id', testId);     

// Assert it
System.assertEquals(testId, ApexPages.currentPage().getParameters().get('id'));

However a better more robust option, that leverages the test controller instance your creating, is to obtain the Id from the StandardController or StandardSetController instance itself within the controller constructor. In this case your code looks a little odd to me since your creating a StandardSetController yet assuming a single 'id' parameter on the URL (which is a valid assumption for a StandardController use case, but not present for StandardSetController based pages). Anyway the following code illustrates how to access the Id information in either case.

// StandardSetController (typically for page controllers listing many records)
Id firstRecordId = stdSetController.getRecords()[0].Id; 

// StandardController (typically for page controllers for a single record)
Id pageRecordId = stdController.getId(); 

If this is part of a test class, querying existing data will fail without annotating the test class. You haven't mentioned that you have taken this step so I'll assume you have not.

Before the class declaration, you will need to annotate @isTest(SeeAllData=true).

Your class will then look like this:

public class MyClass {

But the reality is the proper way to get your test data is to create it in your test method. All test data is rolled back at the end of test method execution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.