0

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){
  try{
   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];
   this.myMap.put('000000000000000','rectype1');
  } catch(System.Exception e){
   ApexPages.addMessages(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:

@isTest
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
     n.getParameters().clear();
     String nextPage = n.getUrl();
     PageReference g = new PageReference('/apex/ConsumableAnalysisWizard');
     g.setRedirect(true);
     String goalPage = g.getUrl();
     System.assertEquals(goalPage, nextPage);
  } 
}

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

 public PageReference redirect(){

 try{
 if(myMap.get(parent.RecordTypeId)=='rectype1'){
   PageReference pref = new PageReference('/apex/ConsumableAnalysisWizard');
   pref.getParameters().putAll(ApexPages.currentPage().getParameters());       
   pref.setRedirect(true);
   return pref;} 
else{ 
   PageReference pref = new PageReference('/apex/rec2ConsumableAnalysisWizard');
   pref.getParameters().putAll(ApexPages.currentPage().getParameters());
   pref.setRedirect(true);
   return pref;    
}} catch(System.Exception e){ApexPages.addMessages(e); return null; } 

 }
0

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;

Test.startTest();
Test.setCurrentPage(pageRef);

//this is the scenario for opening the page upon clicking on button
pageRef.getParameters().put('param1',String.valueOf(param1));



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

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.