This question is partly from curiosity and partly for practical reasons. I have several different custom controllers, and they all take a StandardController as a parameter. Where is the constructor actually called? Or maybe I should say how not where. What I mean is, there is no main method in some class that I have explicitly written which constructs my controller. For instance nothing like this which I have written:

ApexPages.StandardController sc = new ApexPages.StandardController();
RedirectController myController = new RedirectController(sc);

Here is my controllers constructor for reference:

public RedirectController(ApexPages.StandardController controller){
   this.component = (Consumable_Analysis_Component__c)controller.getRecord();
   this.parentId = this.component.Solution_Analysis__c;
   this.parent = [SELECT Id, RecordTypeId FROM Consumable_Analysis__c WHERE Id=:parentId limit 1];
  } catch(System.Exception e){

The VF page must handle this somehow I assume, but how does it set up the standard controller to pass to the custom controller?

In particular I'm interested in this because I'm trying to build a Test Class for my code. The way the code works is it overrides the New button for the Consumable_Analysis_Component__c which are always children objects of Consumable_Analysis__c. The class redirects to other VF pages based on the record type of the parent object. In context the component from above already has the field Solution_Analysis__c because it originated from the parent objects page. But in my Test Class I need to construct this parent object and pass that parameter to the controller somehow. I'm lost as to how to accomplish this. Here's what I have so far for my Test Class, which fails due calling redirect() from a null reference:

public class componentAnalysisWizardTest{
  @isTest public static void testComponentWizards(){

    Consumable_Analysis__c analysis = new Consumable_Analysis__c();
    analysis.RecordTypeId = '000000000000000';
    Consumable_Analysis_Component__c component = new Consumable_Analysis_Component__c();
    component.Solution_Analysis__c = analysis.Id;

     ApexPages.StandardController c = new ApexPages.StandardController(component);
     RedirectController rController = new RedirectController(c);
     PageReference n = rController.redirect();
     //Fails here because n is null
     String nextPage = n.getUrl();
     PageReference g = new PageReference('/apex/ConsumableAnalysisWizard');
     String goalPage = g.getUrl();
     System.assertEquals(goalPage, nextPage);

And here is the redirect() method where null reference is originating:

 public PageReference redirect(){

   PageReference pref = new PageReference('/apex/ConsumableAnalysisWizard');
   return pref;} 
   PageReference pref = new PageReference('/apex/rec2ConsumableAnalysisWizard');
   return pref;    
}} catch(System.Exception e){ApexPages.addMessages(e); return null; } 


You can take this approach to define the currentPage which you are testing. Then create instance of standard controller and pass value to Custom Controller. Finally test controller's method.

PageReference pageRef = Page.VFPage;


//this is the scenario for opening the page upon clicking on button

ApexPages.StandardController std = new Apexpages.StandardController(new Consumable_Analysis_Component__c());            
   RedirectController controller = new RedirectController (std); 
PageReference returnedPg = controller.redirect();

verify returnedPg.getUrl() with assert statement

  • This clarifies things, however I think that I need <code>pageRef</code> to be from a parent object I create, i.e. <code>Consumable_Analysis__c parent = new Consumable_Analysis__c(); PageReference pageRef = new PageReference(parent.Id);</code>, does that make any sense? – Josh D Nov 10 '16 at 21:53
  • I just put a template how to use it – Santanu Boral Nov 10 '16 at 21:56

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.