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
BobPBobP 

Test Class for a apex extension class

I have an apex extension controller that i need to write a test class, but I'm not sure how to write a test class for this type of controller. Any help would be greatly appreciated. My code is below.
 
Public Class AccountExtensionController{
   private Account acc;
   public List<Bids_Sent__c> bidsList {get;set;}
   public Map<String,List<Site_Bid_Details__c>>  bidsMap {get;set;}
   public AccountExtensionController(ApexPages.StandardController sc){
       acc = (Account)sc.getRecord();
       bidsList = new List<Bids_Sent__c>();
       bidsList = [SELECT Id,IsAddedToPDF__c,Customer__r.Service_Agreement_Verbiage__c,Site__c,Site__r.Contract_Start_Date__c,Site__r.Contract_End_Date__c,Site__r.Customer_Location_ID__c,Service_Year__c,Customer__r.Contract_Start_Date__c,Name,Customer__r.Contract_End_Date__c,Site__r.Name,Customer__r.Name,Primary_Contact__r.FirstName,Site__r.BillingCity,Site__r.BillingState,Site__r.BillingStreet,Site__r.BillingPostalCode  FROM Bids_Sent__c WHERE Awarded__c =: acc.Id AND IsAddedToPDF__c=true];
    
    Set<Id> bidId = new  Set<Id>();  
    for(Bids_Sent__c bs : bidsList){
       bidId.add(bs.Id);
    }
     
    bidsMap = new Map<String,List<Site_Bid_Details__c>> ();
    for(Site_Bid_Details__c bd : [SELECT Id, Bid_Name__r.Name,Site__c,Contract_Start_Month__c,Site__r.Customer_Location_ID__c,Cost__c,Customer__r.Contract_Month__c,Increment__c,Total__c,Price__c,Scope__c,Bid_Name__r.Service_Type__c,Number_of_Months__c,Retainer_Fee__c,Monthly_Payment__c,UOM__c  FROM Site_Bid_Details__c WHERE Bid_Name__c IN : bidId]){
        
        if(bidsMap.containsKey(bd.Bid_Name__r.Name)){
  System.debug('CONTAINS KEY: ' + bd.Bid_Name__r.Name);
  bidsMap.get(bd.Bid_Name__r.Name).add(bd);
} else { 
  System.debug('CREATE: ' + bd.Bid_Name__r.Name);
  bidsMap.put(bd.Bid_Name__r.Name,new List<Site_Bid_Details__c>{bd}); 
}
    } 

}

}

 
Kumaresan.ManickamKumaresan.Manickam
There is an existing question similar to your usecase. 

You need to instanctiate a new apexpages.Standardcontroller of specific standard object controller type and assign this as parameter to your extension. This is the main thing.

Test.setCurrentPage(pageRef);
PageReference pageRef = Page.<name of vfpage>;

ApexPages.StandardController sc = new ApexPages.StandardController(new Account(ID=<account id>));
AccountExtensionController testAccPlan = new AccountExtensionController(sc);

Linked article to check: https://developer.salesforce.com/forums/?id=906F00000008y7QIAQ

Choose this reply as best option if it solves your problem.

Cheers!!


        
    
BobPBobP
Thank you Kumaresan,

if I have two visualforce pages using the controller can do the following?
PageReference pageRef = Page.<name of vfpage1>;
PageReference pageRef = Page.<name of vfpage2>;
 
Kumaresan.ManickamKumaresan.Manickam
Yes. But if you have 2 different standard controller pages and its own extension you might need to duplicate a test method for your second page and cover the related logic of each extension individually. Hope this helps.
BobPBobP
Hi Kumaresan,


I'm not having any luck writing this test class. Could you assist me with this entension controller test class?