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;
    System.currentPageReference().getParameters().put('opportunityId', '1234');

EXAMPLE # 1 - Doesn't Work



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

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
    System.currentPageReference().getParameters().put('opportunityId', '1234');  // get no reference to passed in OpportunityId


EXAMPLE # 2 - Works


<apex:page controller="ClassAA">

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.

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);

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.