+ Start a Discussion
seallesssealless 

Testing Controller Extension - why instantiate the VF page?

Just getting started with unit testing a controller extension and I have found a lot of good tips on this board.  I'm still not so sure I understand a testing pattern I see frequently.  Namely, the creation of the PageReference object.  For example here is a simplified version of my extension

 

public class AppointmentControllerExtension {
    
    private ApexPages.StandardController standardController;
    private final Appointment__c appointment;
    
    public AppointmentControllerExtension (ApexPages.StandardController stdController) {
        this.appointment = (Appointment__c)stdController.getRecord();
        this.standardController = stdController;
     }
     
    public String getStudentName() {
    
        String studentName = 'Not Found';
        
        if (appointment.Student__c != null ) {
            Student__c student = [SELECT First_Name__c, Last_Name__c
                                    FROM Student__c
                                   WHERE Id = :appointment.Student__c];
            if (student != null) {
                studentName = student.First_Name__c + ' ' + student.Last_Name__c; 
            }   
        }
        return studentName;
    
    }
    
}

 And the corresponding test class

 

@istest
public class AppointmentControllerExtensionTest {
          
    public static testmethod void testStudentName() {
        
        String firstName = 'First';
        String lastName  = 'Last';
        Student__c student = new Student__c(First_Name__c = firstname, Last_Name__c = lastName);
        insert student;
        
        Appointment__c appointment = new Appointment__c(Student__c = student.Id); 
        insert appointment;
        
        // Instantiate VisualForce Page - but why?
        
        //PageReference pg = Page.Appt_New;
        //Test.setCurrentPage(pg);
        //ApexPages.currentPage().getParameters().put('id', appointment.id);
        
        // Instantiate custom Controller
        
        ApexPages.StandardController stc = new ApexPages.StandardController(appointment);
        AppointmentControllerExtension ae = new AppointmentControllerExtension(stc);
        
        // Test get method
        
        String studentName = ae.getStudentName();
        System.assertEquals(studentName, firstName + ' ' + lastName);
    
    }
   
}

 

I have commented out the references to PageReference and the test runs just fine.  So...what is it that I am missing when I don't include these three lines?  They must serve a purpose, right?

 

Thanks for any insights you can provide.

Best Answer chosen by Admin (Salesforce Developers) 
MandyKoolMandyKool

In your example - You are not passing any query parameters to your page, so setting pageReference is not necessary.

 

If you think about an example where you have to pass say more than 1 parameters to your page; in that case how you are going to test your code?

 

I will give a bit detailed example - 

 

Suppose I have a scenario in which I am getting 2 parameters on my page - Suppose I am calling my page on a click of Button. Once user clicks on a button I am redirecting to my page with 2 fields say accountId and contactID. 

 

Button Url will be something like this - /apex/MyVFPage?accountId={!accountIdToPass}&contactId={!contactIdToPass}

 

Once the user comes to page "MyVFPage" - Url will look something like this - /apex/MyVFPage?accountId=xxxxxx&contactId=yyyyyyy

 

now controller code - 

 

In my controller I m getting the values of accountId and contactId and querying the Account and Contact Records.

Controller code will be something like this 

strAccountId = ApexPages.CurrentPage().getParameters().get('accountId'); //Writing on the fly; ignore syntax

strContactId = ApexPages.CurrentPage().getParameters().get('contacttId'); //Writing on the fly; ignore syntax                       

 

now think about a scenario where I want to test my controller; specially the above two lines. You will find the answer why you have to set the pageReference and pass the parameters to the page.

 

Hope you understood this. Tried my level best!! :)                                                                                                                   

All Answers

MandyKoolMandyKool

Hi,

 

The code simulates, as if you are running the page.  

If your VF page is taking more than one query parameters; you can set the pageReference and pass multiple parameters.

 

eg. If you want to pass accountId='xxxxxxx'&contactId='yyyyyyyyyy' to your page say /apex/MyTestPage?accountId='xxxxxx'&contactId='yyyyyyyy'

 

In this scenario you dont have anyway to test in your test method unless and until you set your page and passs the correct parameters to your page.

 

Hope this answers your question

seallesssealless

I don't know what you mean when you say I don't have anyway to test in the test method.  This test runs successfully and tests the method in question.

MandyKoolMandyKool

In your example - You are not passing any query parameters to your page, so setting pageReference is not necessary.

 

If you think about an example where you have to pass say more than 1 parameters to your page; in that case how you are going to test your code?

 

I will give a bit detailed example - 

 

Suppose I have a scenario in which I am getting 2 parameters on my page - Suppose I am calling my page on a click of Button. Once user clicks on a button I am redirecting to my page with 2 fields say accountId and contactID. 

 

Button Url will be something like this - /apex/MyVFPage?accountId={!accountIdToPass}&contactId={!contactIdToPass}

 

Once the user comes to page "MyVFPage" - Url will look something like this - /apex/MyVFPage?accountId=xxxxxx&contactId=yyyyyyy

 

now controller code - 

 

In my controller I m getting the values of accountId and contactId and querying the Account and Contact Records.

Controller code will be something like this 

strAccountId = ApexPages.CurrentPage().getParameters().get('accountId'); //Writing on the fly; ignore syntax

strContactId = ApexPages.CurrentPage().getParameters().get('contacttId'); //Writing on the fly; ignore syntax                       

 

now think about a scenario where I want to test my controller; specially the above two lines. You will find the answer why you have to set the pageReference and pass the parameters to the page.

 

Hope you understood this. Tried my level best!! :)                                                                                                                   

This was selected as the best answer
seallesssealless

This explains why my tests work regardless of the use of PageReference, etc.  Thank you for clearing that up.

 

So, is this the only reason to use this 

 

PageReference pg = Page.Appt_New;
Test.setCurrentPage(pg);
ApexPages.currentPage().getParameters().put('id', appointment.id);

 for setting get parameters?  Seems pretty cumbersome if that is all it is good for.

MandyKoolMandyKool

Yes Exactly!!!

 

Though its cumbersome; it works like a charm :).

Welcome to the world of "CLOUD Developers". Will see you here!!