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 visualforce page extension controller

I have a extension contrlloer for a visualforce page that i need to create a simple text class, but i'm not sure how to accomplish this. Any help would be greatly appreciated. My apex controller is below.
 
Public Class AccountExtensionController{
   private Account acct;
   public List<Bids_Sent__c> bidsList {get;set;}
   public Map<String,List<Site_Bid_Details__c>>  bidsMap {get;set;}
   public AccountExtensionController(ApexPages.StandardController sc){
       acct = (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 =: acct.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,Site__r.Customer_Location_ID__c,Cost__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}); 
   }
 } 

}

}

 
Best Answer chosen by BobP
Christian Schwabe (x)Christian Schwabe (x)
Hi Bop,

I would suggest the following testclass:
private class AccountExtensionControllerTest {
    
    @isTest
    private static void testStandardController(){

        //Create objects you need for testing purposes.
        //...
        
        Account account = new Account();
        account.Name='Test GmbH';
        insert account;

        ApexPages.StandardController standardController = new ApexPages.StandardController(testAccountPlanInsert);
        AccountExtensionController accountExtensionController = new AccountExtensionController(standardController);
        
        PageReference pageReference = Page.<Visualforce-Page>;//<Is a placeholder>
        pageReference.getParameters().put('id', String.valueOf(testAccountPlanInsert.Id));
        Test.setCurrentPage(pageReference);
    }
}




Regards,
Christian

All Answers

Christian Schwabe (x)Christian Schwabe (x)
Hi Bop,

I would suggest the following testclass:
private class AccountExtensionControllerTest {
    
    @isTest
    private static void testStandardController(){

        //Create objects you need for testing purposes.
        //...
        
        Account account = new Account();
        account.Name='Test GmbH';
        insert account;

        ApexPages.StandardController standardController = new ApexPages.StandardController(testAccountPlanInsert);
        AccountExtensionController accountExtensionController = new AccountExtensionController(standardController);
        
        PageReference pageReference = Page.<Visualforce-Page>;//<Is a placeholder>
        pageReference.getParameters().put('id', String.valueOf(testAccountPlanInsert.Id));
        Test.setCurrentPage(pageReference);
    }
}




Regards,
Christian
This was selected as the best answer
ravi raiyaravi raiya
I think the above is the best solution to your problem.
MP Jan Kalyan Yojana (https://onlineyojana.in/mp-jan-kalyan-yojana-online-apply-and-status/)