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
Lee_CampbellLee_Campbell 

Test Class for simple Controller Extension

Hi folks,

 

I've written a really simple controller extension for a custom object, that ensures the "target" of my Visualforce page is only ever one record (there only exists one record for this object, it's a kind of "joining object").


The class is below, but I'm struggling to get my head round what my test class should try and invoke. Any help massively appreciated!

 

Thanks,

Lee

 

public class BPT_SummaryViewController{
    public BPT_SummaryViewController(ApexPages.StandardController projcon){}
    
    public Bid_Planning_Summary__c getSummary(){
        Bid_Planning_Summary__c a = [select id from Bid_Planning_Summary__c][0];
        return a;
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
GsajeeshGsajeesh

Hi Lee,

 

Say Employee is your custom object type.

Hope the below code helps!

 

@isTest(SeeAllData=false)
    static void myUnitTest() {
     Employee e = new Employee(name='Lee', Dept='IT');
insert e;
           
            test.startTest();
            ApexPages.currentPage().getParameters().put('id',e.id);
            ApexPages.StandardController sc = new ApexPages.StandardController(e);
            BPT_SummaryViewController bptCon = new BPT_SummaryViewController(sc); 
            bptCon.getSummary();
//write your system assert statements to check your results
            test.stopTest(); 
            }     
     }

All Answers

GsajeeshGsajeesh

Hi Lee,

 

Say Employee is your custom object type.

Hope the below code helps!

 

@isTest(SeeAllData=false)
    static void myUnitTest() {
     Employee e = new Employee(name='Lee', Dept='IT');
insert e;
           
            test.startTest();
            ApexPages.currentPage().getParameters().put('id',e.id);
            ApexPages.StandardController sc = new ApexPages.StandardController(e);
            BPT_SummaryViewController bptCon = new BPT_SummaryViewController(sc); 
            bptCon.getSummary();
//write your system assert statements to check your results
            test.stopTest(); 
            }     
     }
This was selected as the best answer
Lee_CampbellLee_Campbell

Brilliant, with the appropriate modifications to match my custom object, I got 100% code coverage and my first ever Visualforce page is working fine.

 

Thank you very much!