function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Patrick J O'SullivanPatrick 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:
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;
        }
}


Best Answer chosen by Patrick J O'Sullivan
logontokartiklogontokartik
Ok. For any test class, you might have already known from documentation that you need to create your own test data. 


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 

Key_Account_Overview__c testKeyAccount = new Key_Account_Overview__c();
// Make sure you populate all fields you need.
insert testKeyAccount;

Apexpages.StandardController testCon = new Apexpages.StandardController(testKeyAccount);

// Now you initialize your Apex CLass, with the testCon

KAMKAPextension testExt = new KAMKAPextension(testCon); // This will call your Constructor.

// Now its all simple calling the methods.


List<Kam_Plan_Actions__c> overdues = testExt.getOverDue();

// Make sure you assert what you think would be returned with what is being returned.

List<Kam_Plan_Actions__c> cancelled =  testExt.getcancelled();



Hope this helps.

Also FYR, http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm




All Answers

logontokartiklogontokartik
Ok. For any test class, you might have already known from documentation that you need to create your own test data. 


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 

Key_Account_Overview__c testKeyAccount = new Key_Account_Overview__c();
// Make sure you populate all fields you need.
insert testKeyAccount;

Apexpages.StandardController testCon = new Apexpages.StandardController(testKeyAccount);

// Now you initialize your Apex CLass, with the testCon

KAMKAPextension testExt = new KAMKAPextension(testCon); // This will call your Constructor.

// Now its all simple calling the methods.


List<Kam_Plan_Actions__c> overdues = testExt.getOverDue();

// Make sure you assert what you think would be returned with what is being returned.

List<Kam_Plan_Actions__c> cancelled =  testExt.getcancelled();



Hope this helps.

Also FYR, http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm




This was selected as the best answer
Patrick J O'SullivanPatrick J O'Sullivan
Thank you so much, That Clears it up nicely just couldn't get my head around the link you provided me, I've ended up there about 2-3 times over the last couple of days alone.