+ Start a Discussion
pdostapdosta 

Help with Test Method

I need help with writing a test method for a class that creates opens a Visualforce page with predetermined parameters.  I have no idea where to begin if anyone can steer me in the right direction.

 

Here is the class...how do I write the method to test this?

 

 

public class psuNewCurrency
{
        private final Participant_ID_Request__c request;
        private final Participant_ID__c objParticipant_Id;
        public String Participant_ID;
        public String Participant_ID_id;
        public String Request_ID;
        public String Request_Name;
        public String Record_Type;
        
public psuNewCurrency(ApexPages.StandardController controller)
{
    this.objParticipant_Id=[select Id, Name, Participant_ID_Generation__c  from Participant_ID__c where Name=:System.CurrentPageReference().getParameters().get('CF00NS0000000O8dw')];
    Participant_ID = System.CurrentPageReference().getParameters().get('CF00NS0000000O8dw');
    Record_Type = System.CurrentPageReference().getParameters().get('RecordType');
    Participant_ID_id = objParticipant_Id.Id;
    Request_ID = objParticipant_Id.Participant_ID_Generation__c;
    this.request=[select Id, Name  from Participant_ID_Request__c where Id= :Request_ID ];
    Request_Name = request.Name; 
    
}
public Pagereference pageredir()
{
    
        Pagereference newpage = new Pagereference ('/a0O/e?CF00NS0000000O8dw='+ Participant_ID 
                    +'&CF00NS0000000O8dw_lkid='+ Participant_ID_id 
                    +'&CF00NS0000000OAKq_lkid='+ Request_ID
                    +'&CF00NS0000000OAKq='+ Request_Name 
                    +'&retURL='+ Participant_ID_id
                    +'&RecordType='+ Record_Type
                    +'&_CONFIRMATIONTOKEN=fzXMPMYOtRvbVHrmy6.3rdbroMrAH.pxVXb2iB71kL.oPdjgcmNKyWCeyGwgB68fQ7wBH_Zv5wDZyDMNz1NAt562.Ll8FVIhhjSmAuY6c9d8a2SLLWoZlctxQxROV.l7RHe32ME9Xr9XLz2y3aX7UmhlCYEe4RPZiluYf979pzFIyH3l' 
                    +'&cancelURL='+ Participant_ID_id
                    +'&ent=01IS00000008Xhj' //Currency Relationship Object
                    +'&nooverride=1');
    return newpage;
 
}
}

 

 

mtbclimbermtbclimber

If that's the whole class then your method needs to set some parameters in the request:

 

ApexPages.PageReference testpage = Page.NameOfThisPage;
testPage.getParameters().put('param1','value1');
Test.setCurrentPage(testpage);

 

instantiate the class:

 

 

ApexPages.StandardController con = new ApexPages.StandardController(new ?????()); //not sure what sobject type to use here, you dont seem to call it.
psuNewCurrency pnc = new psuNewCurrency(con);

 

Then call the method on the class:

 

 

ApexPages.PageReference p = pnc.pageredir();

 

 

And finally you need to assert that the url and params are correct:

 

 

System.assertEquals(p.getParameters().get('paramX'),'expectedvalue','Oops, paramX did not get the expected value following the pageredir method invocation');

 

 

Now a couple of things to know here.  I'm assuming that what's going on here is that you've overridden the new action for your custom object with a Visualforce page that uses this controller extension and binds the page component's action attribute to the pageredir() method.  If so, then the stuff you are doing in your constructor to sniff request parameters is unnecessary.

 

The extension model required here is there for a reason, from the standardcontroller you can get a typed sobject for you to work with that includes defaulted values that are set by salesforce.com params. So in your case the constructor would look like this:

 

public psuNewCurrency(ApexPages.StandardController controller) {

objParticipant_Id = (Participant_ID__c)controller.getRecord();
/*
This query isn't required if the page contains references to the fields in the select. If it does then
you can replace the param reference with :objParticipant_Id.id

this.objParticipant_Id=[select Id, Name, Participant_ID_Generation__c from Participant_ID__c where Id=:objParticipant_Id.id];
*/

/* remove this line and the variable, its duplicative. The value you need is simply "objParticipant_Id.id"
Participant_ID = System.CurrentPageReference().getParameters().get('CF00NS0000000O8dw');
*/

/*
The record type is also supplied in the given object. So you can just remove this:
Record_Type = System.CurrentPageReference().getParameters().get('RecordType');

as you can get the record type when you need it off of the object like this: "objParticipant_Id.recordtypeid"
*/

/*
you dont need this additional variable
Participant_ID_id = objParticipant_Id.Id;
*/

Request_ID = objParticipant_Id.Participant_ID_Generation__c;
/*

You don't need the name value in a target Visualforce page.
this.request=[select Id, Name from Participant_ID_Request__c where Id= :Request_ID ];
Request_Name = request.Name;

*/

}

 

As for what you are doing in the redirection method yourself you need to recognize that you are programming against observed but not supported behavior. There is no guarantee any release of salesforce.com  won't change how we name or process any of the parameters you are setting in the pageredir() method.  The supported approach is to just present a form with this Visualforce page rather than use it to redirect. Yes this means you lose the page layout editor customization of the new form but it's the only way to guarantee against backwards compatibility issues you are subject to with this approach. Proceed as you must but do so with caution.

 

 

 

pdostapdosta

Andrew, this was absolutely marvelous!  Here is my new code:

 

Class

 

public class psuNewCurrency
{
private final Participant_ID_Request__c request;
private final Participant_ID__c objParticipant_Id;
public String Participant_ID;
public String Participant_ID_id;
public String Request_ID;
public String Request_Name;
public String Record_Type;

public psuNewCurrency(ApexPages.StandardController controller) {

objParticipant_Id = (Participant_ID__c)controller.getRecord();
Request_ID = objParticipant_Id.Participant_ID_Generation__c;

}
public Pagereference pageredir()
{

Pagereference newpage = new Pagereference ('/a0O/e?CF00NS0000000O8dw='+ Participant_ID
+'&CF00NS0000000O8dw_lkid='+ Participant_ID_id
+'&CF00NS0000000OAKq_lkid='+ Request_ID
+'&CF00NS0000000OAKq='+ Request_Name
+'&retURL='+ Participant_ID_id
+'&RecordType='+ Record_Type
+'&_CONFIRMATIONTOKEN=fzXMPMYOtRvbVHrmy6.3rdbroMrAH.pxVXb2iB71kL.oPdjgcmNKyWCeyGwgB68fQ7wBH_Zv5wDZyDMNz1NAt562.Ll8FVIhhjSmAuY6c9d8a2SLLWoZlctxQxROV.l7RHe32ME9Xr9XLz2y3aX7UmhlCYEe4RPZiluYf979pzFIyH3l'
+'&cancelURL='+ Participant_ID_id
+'&ent=01IS00000008Xhj' //Currency Relationship Object
+'&nooverride=1');
return newpage;

}
}

 Test

 

public class psuNewCurrencyTest
{
    static testmethod void testNewCurrency()
    {
        ApexPages.PageReference testpage = Page.psunewcurrency;
        testPage.getParameters().put('Participant_ID','001');
        Test.setCurrentPage(testpage);
        
        ApexPages.StandardController con = new ApexPages.StandardController(new Participant_ID__c());
        psuNewCurrency pnc = new psuNewCurrency(con);
        
        ApexPages.PageReference p = pnc.pageredir();
        
        System.assertEquals(p.getParameters().get('Participant_ID'),'001','Oops, Participant_ID did not get the expected value following the pageredir method invocation');
    }
}

 

 

However, when I run the test, my assertion fails when I get the expected result (001)


Method Name
Total Time (ms)
Message
Stack Trace
psuNewCurrencyTest.testNewCurrency33.0System.Exception: Assertion Failed: Oops, Participant_ID did not get the expected value following the pageredir method invocation: Expected: null, Actual: 001Class.psuNewCurrencyTest.testNewCurrency: line 14, column 9 External entry point

 

Am I doing something wrong here?

 

public class psuNewCurrencyTest
{
    static testmethod void testNewCurrency()
    {
        ApexPages.PageReference testpage = Page.psunewcurrency;
        testPage.getParameters().put('Participant_ID','001');
        Test.setCurrentPage(testpage);
        
        ApexPages.StandardController con = new ApexPages.StandardController(new Participant_ID__c());
        psuNewCurrency pnc = new psuNewCurrency(con);
        
        ApexPages.PageReference p = pnc.pageredir();
        
        System.assertEquals(p.getParameters().get('Participant_ID'),'001','Oops, Participant_ID did not get the expected value following the pageredir method invocation');
    }
}