+ Start a Discussion
Rick MacGuiganRick MacGuigan 

Unit testing extended controllers

I have 8 visualforce pages that all contain a relatively simple extended APEX controller handing things like save, pagereferences and SOQL to derive fields from a parent object. This is my first unit testing exercise. Do I just need to test each extended controller ? All of the controllers are very similar to function just extending different standard controllers. Also, the visualforce pages rely on upper level objects Account | Audt (custom object.) the first visualforce page (Sample Policy - custom object) is created from the 'Audit' (custom object .) Subsequent visualforce pages are created as related list children of the Sample Policy object. SOQl queries are used to pull in fields from the grandparent (Audit) object for rendering control.  

Also, would appreciate any suggestions on how to best test the SOQL response for null or more than 1 resulting record.

Below is a sample.  Would appreciate any guidance. Thanks.

public with sharing class ParentChildExtensionDRIVERsample_New {


Receives parent ID (accID) aandd Sample ID passed via commandbutton or custom button on parent related list.
AccId must be passed via URL since we are instantiating a new object reccord which is not saved.
accID is needed to query parent fields that are used on the visualforce page for rendering.
SampleId is needed to load the related field on the object. 
A list object is used for the SOQL query to the parent. The result set is used in the visualforce page for setting control variables for rendering. 
The Sample ID is loaded into the related list field on the instanitated object to establish relationship when saved. 

public Audit__c driver{get;set;}
Auto_Drivers__c record;
private ApexPages.StandardController controller;

String accId;
String sampleId;

public ParentChildExtensionDRIVERsample_New(ApexPages.StandardController standardController)
 this.controller = standardController;
//set sample to current record.
this.record = (Auto_Drivers__c)standardController.getrecord();


List< Audit__c> drivers = [ select Id, Driver_Information__c, Age__c, Gender__c, Marital_Status__c, Named_Insured_Add_Driver__c, Occupation__c, Business_Use__c                
           from Audit__c where Id = :ApexPages.currentPage().getParameters().get('accId')]; 
If (drivers.isEmpty() == false) driver = drivers[0];

//Load the parent Name (ID) into the Audit field for newly instantiated object record. 
record.Auto_Audit_Sample_Policy__c = ApexPages.currentPage().getParameters().get('sampleId');
//return user back to the audit sample to add new record.
public PageReference saveAndReturn()
     PageReference pr = Page.PROD_Audit_Auto_PolicySample_Open;
     pr.getParameters().put('id', sampleId);
     return pr;

Suresh RaghuramSuresh Raghuram
Unlike triggers vfpages and the controller or Extension classes will not get covered when you create test data and after doing DML operations. you need to initate the classes,  and call them in the test class and same with pages, search on google you will find lot of examples.
You can go thru this as well for getting some idea.