You need to sign in to do that
Don't have an account?
Patrick J O'Sullivan
Need Help Writing a test Class for controller Extension please
Hi Guys,
I am Studying Apex and VF at the moment but My boss won't wait for me to finish my studies before forcing me to develop,
I've got the basics pretty well and have created a controller extension and page and now need to deploy from sandbox but I don't know how to test a controller extension and I can't figure it out from the 501 learning materials.
The contoller is simply pulling KAM plan actions into list variables based on time and status, this is to allow a contextual quick look feature for our Head of sales when reviewing Key account management synopses.
Could someone please using my controller below explain to me how I go about testing this? I am afraid I am really struggling to understand testing VF controllers and extensions.....
My Page is Called KAMBasic
My controller is:
I am Studying Apex and VF at the moment but My boss won't wait for me to finish my studies before forcing me to develop,
I've got the basics pretty well and have created a controller extension and page and now need to deploy from sandbox but I don't know how to test a controller extension and I can't figure it out from the 501 learning materials.
The contoller is simply pulling KAM plan actions into list variables based on time and status, this is to allow a contextual quick look feature for our Head of sales when reviewing Key account management synopses.
Could someone please using my controller below explain to me how I go about testing this? I am afraid I am really struggling to understand testing VF controllers and extensions.....
My Page is Called KAMBasic
My controller is:
public class KAMKAPextension { private final Key_Account_Overview__c KAP; List<Kam_Plan_Actions__c> overdue; List<Kam_Plan_Actions__c> next30days; List<Kam_Plan_Actions__c> from30to60days; List<Kam_Plan_Actions__c> from60to90days; List<Kam_Plan_Actions__c> longterm; List<Kam_Plan_actions__c> Completed; List<Kam_Plan_actions__c> cancelled; list<Kam_Plan_actions__c> KamToday; //Method for new Child Kam Plan Action goes here public KAMKAPextension(ApexPages.StandardController KAPController) { this.KAP = (Key_Account_Overview__c)KAPController.getRecord(); } Public list<Kam_Plan_Actions__c> getoverdue() { If (overdue == null) { overdue = [ SELECT Id, Name, Status__c, Due_Date__c FROM Kam_Plan_Actions__c WHERE due_date__c < TODAY AND Status__c !='Complete' AND Status__c !='Cancelled' AND Key_Account_Synopsis__c = :KAP.id]; } return overdue; } Public list<Kam_Plan_Actions__c> getKamToday() { If (KamToday == null) { KamToday = [ SELECT Id, Name, Status__c, Due_Date__c FROM Kam_Plan_Actions__c WHERE due_date__c = NEXT_N_DAYS:29 AND Status__c !='Complete' AND Status__c !='Cancelled' AND Key_Account_Synopsis__c =:KAP.id]; } return KamToday; } Public list<Kam_Plan_Actions__c> getnext30days() { If (next30days == null) { next30days = [ SELECT Id, Name, Status__c, Due_Date__c FROM Kam_Plan_Actions__c WHERE due_date__c >= TODAY AND due_date__c = NEXT_N_DAYS:29 AND Status__c !='Complete' AND Status__c !='Cancelled' AND Key_Account_Synopsis__c =:KAP.id]; } return next30days; } Public list<Kam_Plan_Actions__c> getfrom30to60days() { If (from30to60days == null) { from30to60days = [ SELECT Id, Name, Status__c, Due_Date__c FROM Kam_Plan_Actions__c WHERE due_date__c !=NEXT_N_DAYS:29 AND due_date__c = NEXT_N_DAYS:59 AND Status__c !='Complete' AND Status__c !='Cancelled' AND Key_Account_Synopsis__c = :KAP.id]; } return from30to60days; } Public list<Kam_Plan_Actions__c> getfrom60to90days() { If (from60to90days == null) { from60to90days = [ SELECT Id, Name, Status__c, Due_Date__c FROM Kam_Plan_Actions__c WHERE due_date__c !=NEXT_N_DAYS:59 AND due_date__c = NEXT_N_DAYS:89 AND Status__c !='Complete' AND Status__c !='Cancelled' AND Key_Account_Synopsis__c = :KAP.id]; } return from60to90days; } Public list<Kam_Plan_Actions__c> getlongterm() { If (longterm == null) { longterm = [ SELECT Id, Name, Status__c, Due_Date__c FROM Kam_Plan_Actions__c WHERE due_date__c > TODAY AND due_date__c != TODAY AND due_date__c !=NEXT_N_DAYS:89 AND Status__c !='Complete' AND Status__c !='Cancelled' AND Key_Account_Synopsis__c = :KAP.id]; } return longterm; } Public list<Kam_Plan_Actions__c> getCompleted() { If (Completed == null) { Completed = [ SELECT Id, Name, Status__c, Due_Date__c FROM Kam_Plan_Actions__c WHERE due_date__c >NEXT_N_DAYS:89 AND Status__c ='Complete'AND Key_Account_Synopsis__c = :KAP.id]; } return Completed; } Public list<Kam_Plan_Actions__c> getcancelled() { If (cancelled == null) { cancelled = [ SELECT Id, Name, Status__c, Due_Date__c FROM Kam_Plan_Actions__c WHERE Status__c ='Completed' AND Key_Account_Synopsis__c =:KAP.id ]; } return cancelled; } }
So in your test class the first thing you do is create test data for all the objects you are referencing in your Apex class.
Then for Controller extensions all you do is build a test INstance of Apexpages.StandardController using the record you created, in your example it would be Key_Account_Overview__c.
So the code to initialize would be
Hope this helps.
Also FYR, http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm
All Answers
So in your test class the first thing you do is create test data for all the objects you are referencing in your Apex class.
Then for Controller extensions all you do is build a test INstance of Apexpages.StandardController using the record you created, in your example it would be Key_Account_Overview__c.
So the code to initialize would be
Hope this helps.
Also FYR, http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm