0

I have a VFP called myUIPage that has some input fields, some of which are bound to the Opportunity object ("standardController='Opportunity'"). There is also some functionality for this page that occurs in ClassA ("extension='ClassA'). My logic works, the VFP does what it needs to do. When creating my unit test, to get code coverage, I need to be able to set a variable in ClassA that is a parameter that is ?id=XXX. In example two below (which works) I am able to accomplish this by using

    PageReference pageRef = Page.myUIPage;
    Test.setCurrentPageReference(pageRef);
    System.currentPageReference().getParameters().put('opportunityId', '1234');

EXAMPLE # 1 - Doesn't Work

VFP:

myUIPage.vfp

<apex:page standardController="Opportunity" extension="ClassA">
</apex:page>

public virtual class ClassA{
    public ClassA(){}
    public ClassA(ApexPages.StandardController controller){}
    public string opportunityId {get; set;}
}

Everything there is OKAY.

Now, adding Test Class

public class ClassA_Tests extends ClassA{
    ClassA_Tests (){}

    // problem here
    PageReference pageRef = Page.myUIPage; // doen't seem to work here
    Test.setCurrentPageReference(pageRef);
    System.currentPageReference().getParameters().put('opportunityId', '1234');  // get no reference to passed in OpportunityId


}

EXAMPLE # 2 - Works

myUIPage2.vfp

<apex:page controller="ClassAA">
</apex:page>

public virtual class ClassAA{
    // no constructor...
    // do work...
}

In the test class for ClassAA

public class ClassAA_Test extends ClassAA {
    // works perfectly!
    PageReference pageRef = Page.myUIPage2;
    Test.setCurrentPageReference(pageRef );
    System.currentPageReference().getParameters().put('opportunityId', '1234');
    // I am able to send values to my class from the test class.
}

1 Answer 1

0

Add this to the end of your first test.

Opportunity opp = new Opportunity(); //here you can query for one
Apexpages.StandardController stdctr = new Apexpages.StandardController(opp);
ClassA ctr = new ClassA(stdctr);

You must log in to answer this question.

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