I'm new to using APEX and would really appreciate your help. I found this bit of code online (http://salesforcesource.blogspot.co.uk/2008/10/how-to-create-custom-related-list-on.html)and modified the class and visualforce page to suit what I need but now I need to write a test class to ensure it has coverage for deployment. Can someone please help me construct a test class, or point me in the right direction? Thanks, Dan.

My class currently looks like:

public class sharesProductHistory
    private List<Product_History__c> sharesph;
    private Contact cntact; 
    public sharesProductHistory(ApexPages.StandardController controller) 
        this.cntact= (Contact)controller.getRecord();

    public List<Product_History__c> getsharesph()

        sharesph = [SELECT Id, Contact__c, CreatedById, CreatedDate, Date_Created__c, End_Issue_End_Date__c, End_Issue_Start_Date__c, End_Issue__c, ID__c, IsDeleted, Issue_Length__c, LastModifiedById, LastModifiedDate, Name, Opportunity__c, OwnerId, Price__c, Product_Type__c, RecordTypeId, Share_Advisor_Customer_ID__c, Start_Issue_End_Date__c, Start_Issue_Start_Date__c, Start_Issue__c, Status__c, SystemModstamp, Type__c FROM Product_History__c 
where Type__c = 'Share Advisor' and Contact__c = :cntact.id];
        return sharesph;

For reference, my visual force page looks like:

<apex:page standardController="Contact" extensions="sharesProductHistory">

<apex:form >

<apex:pageblock id="CustomList" title="Related Opportunities 1"  >
   <apex:pageBlockTable value="{!sharesph}" var="o" rendered="{!NOT(ISNULL(sharesph))}">
        <apex:column value="{!o.Date_Created__c}"/>
   <apex:outputLabel value="No records to display" rendered="{!(ISNULL(sharesph))}" styleClass="noRowsHeader"></apex:outputLabel>



1 Answer 1


Here is an example of testing a class that uses a standard controller

@isTest sharesProductHistoryTest{
    public static void unitTest(){
        ApexPages.StandardController sc = new ApexPages.StandardController(ObjectStandardControllerIsFor);
        sharesProductHistory sph = new sharesProductHistory (sc);

        PageReference pageRef = Page.PageName;
        pageRef.getParameters().put('id', String.valueOf(ObjectStandardControllerIsFor.Id));



This first creates the StandardController of the object you wish to use and then creates the extension that you created passing in the standard controller. It then passes the ID of the exact object you wish to use into the parameters of the page and then sets the page as the current page. You can then make any subsequent method calls to your SharesProductHistory variable sph.

Note: You will not be able to access any actual records in the database unless the SeeAllData flag is set to True. You need to create any objects that you will be querying for before calling the method that queries for an object.

  • Thanks, Chris! Your test class worked perfectly and I have since been able to deploy my classes and visual force pages. Dan
    – Daniel G
    Oct 18, 2013 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.