Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a Extension Controller which gets back a different set of Opportunities based on the value of HTTP parameter named display sent in. So my code is as such:

public OpportunityListExtension(ApexPages.StandardSetController stdController) {
    // blank constructor
}

public List<Opportunity> getOpportunities() {
    PageReference pageRef = ApexPages.currentPage();
    String display = pageRef.getParameters().get('display');
    String whereclause = '';
    if (display == (LOST)) {
        ...
    } else if (display.equals(INACTIVE)) {
        ...

Based on the the value of display I return different Opportunities. Works great but I need a unit test for this and I am not sure how to pass in the display value to the controller.

Here is my test:

@isTest
public static void testOpportunityListExtension() {
    PageReference pageRef = Page.Opportunities;
    Test.setCurrentPageReference(pageRef);  
    // create an opp
    Opportunity opp = new Opportunity(name='testOpp');
    opp.stageName = 'Closed Won';
    opp.CloseDate = Date.today() - 4;
    List<Opportunity> opps = new List<Opportunity>();
    opps.add(opp);
    insert opps;

    ApexPages.StandardSetController sc = 
             new ApexPages.StandardSetController(new Opportunity[]{});
    //
    // ?????? Not sure how to pass in display value
    OpportunityListExtension ole = new OpportunityListExtension(sc);
    List<Opportunity> oppsFromController = ole.getOpportunities();        

    System.assertEquals(1, oppsFromController.size());
    Opportunity oppportunity = opps.get(0); 
}
share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

The function getParameters() returns a Map<String, String> to which you can add items. In your test you can use something like this:

PageReference pageRef = Page.Opportunities;
pageRef.getParameters().put('display', 'INACTIVE');
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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