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
Scott0987Scott0987 

test class for visualforce page

I have a visualforce page and cannot get the test page to work.  Here is my visualforce page. 

<apex:page standardController="Ticket__c" extensions="TicketExtension">
<style type="text/css">
.odd{background-color: #DBDBDB;} 
.even{background-color:#E4E4E4;}
</style>
<apex:pageBlock title="Open Tickets"  >
<apex:dataTable value="{!openTickets}" var="OT" columnsWidth="134px, 250px, 61px, 250px, 82px, 250px, 103px" rowClasses="odd,even">
<apex:column >Brief Ticket Description:</apex:column><apex:column value="{!OT.Brief_Description__c}" />
<apex:column >CC Email:</apex:column><apex:column value="{!OT.CC_Email__c}"/>
<apex:column >Ticket Details:</apex:column><apex:column value="{!OT.Request_Description__c}"/>
<apex:column >Ticket Created By:</apex:column><apex:column value="{!OT.CreatedBy.name}"/>
</apex:dataTable>
<apex:form >
<apex:commandButton value="New Ticket" action="{!redirectNewTicket}"/>
</apex:form>
</apex:pageBlock>
<apex:pageBlock title="Closed Tickets">
<apex:dataTable value="{!closedTickets}" var="CT" columns="3" columnsWidth="134px, 250px, 61px, 250px, 82px, 250px, 103px" rowClasses="odd,even">
<apex:column >Brief Ticket Description:</apex:column><apex:column value="{!CT.Brief_Description__c}" />
<apex:column >CC Email:</apex:column><apex:column value="{!CT.CC_Email__c}"/>
<apex:column >Ticket Details:</apex:column><apex:column value="{!CT.Request_Description__c}"/>
<apex:column >Ticket Created By:</apex:column><apex:column value="{!CT.CreatedBy.name}"/>
</apex:dataTable>
</apex:pageBlock>
</apex:page>

 here is my controller

public with sharing class TicketExtension {

    public TicketExtension(ApexPages.StandardController controller) {
        this.user_object = (CAG_tickets__Ticket__c)Controller.getRecord();
    }

    //Shows all open tickets
    public list<CAG_tickets__Ticket__c> getopenTickets(){
        return [select CAG_tickets__Brief_Description__c, CreatedBy.name, CAG_tickets__CC_Email__c, CAG_tickets__Request_Description__c, CAG_tickets__How_Urgent_is_the_Request__c from CAG_tickets__Ticket__c where CAG_tickets__Is_Closed__c = false];
    }
    
    //Shows all closed tickets
    public list<CAG_tickets__Ticket__c> getclosedTickets(){
        return [select CAG_tickets__Brief_Description__c, CreatedBy.name, CAG_tickets__CC_Email__c, CAG_tickets__Request_Description__c, CAG_tickets__How_Urgent_is_the_Request__c from CAG_tickets__Ticket__c where CAG_tickets__Is_Closed__c = true];
    }

    //section to create a new ticket.
    public string ccEmail {get;set;}
    public string briefDescrip {get;set;}
    public string descrip {get;set;}
    public string ur {get;set;}
    
    public PageReference createNewTicket(){
        CAG_tickets__Ticket__c t = new CAG_tickets__Ticket__c(CAG_tickets__Brief_Description__c=briefDescrip, CAG_tickets__CC_Email__c=ccEmail, CAG_tickets__How_Urgent_is_the_Request__c=ur, CAG_tickets__Request_Description__c=descrip);
        insert t;
        PageReference pageRef = new PageReference('https://cag-tickets.na11.visual.force.com/apex/tickets');
        return pageRef;
    }
    
    //Redirects the new ticket button to the new ticket apex page
    public PageReference redirectNewTicket(){
        PageReference pageRef = new PageReference('/apex/newticket');
        return pageRef;
    }
    
    
    
    
    //this section populates the options field with the picklist values.
    private final CAG_tickets__Ticket__c user_object;
    public List<selectOption> getPickValues(Sobject object_name, String field_name) {
        List<selectOption> options = new List<selectOption>(); //new list for holding all of the picklist options
        Schema.sObjectType sobject_type = object_name.getSObjectType(); //grab the sobject that was passed
        Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); //describe the sobject
        Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); //get a map of fields for the passed sobject
        List<Schema.PicklistEntry> pick_list_values = field_map.get(field_name).getDescribe().getPickListValues(); //grab the list of picklist values for the passed field on the sobject
        for (Schema.PicklistEntry a : pick_list_values) { //for all values in the picklist list
            options.add(new selectOption(a.getLabel(), a.getValue())); //add the value and label to our final list
        }
        return options; //return the List
    }
    
    //picklist values
    public List<selectOption> geturList(){
        return getPickValues(user_object, 'How_Urgent_is_the_Request__c');
    }

    
    




}

 And here is the test method I have written.

public class testTicketsPage {
    static testMethod void testMyTicketsPage(){
    
    CAG_tickets__Ticket__c t = new CAG_tickets__Ticket__c(CAG_tickets__Brief_Description__c='brief description', CAG_tickets__CC_Email__c='cc@email.com', CAG_tickets__How_Urgent_is_the_Request__c='1 - This would be nice to have.', CAG_tickets__Is_Closed__c=True, CAG_tickets__Request_Description__c='Request Description');
    insert t;
    
    CAG_tickets__Ticket__c t1 = new CAG_tickets__Ticket__c(CAG_tickets__Brief_Description__c='brief description', CAG_tickets__CC_Email__c='cc@email.com', CAG_tickets__How_Urgent_is_the_Request__c='1 - This would be nice to have.', CAG_tickets__Is_Closed__c=False, CAG_tickets__Request_Description__c='Request Description');
    insert t1;
    
    PageReference ref = new PageReference('/apex/tickets');
    test.setCurrentPage(ref);    
    
    TicketExtension con = new TicketExtension();    
    con.getopenTickets();
}
}

 I am new to testing visualforce pages.  the issue I seem to be having is that when I run the test the debug log says "Attempt to de-reference a null object".  Any idea on what I am doing wrong?

Best Answer chosen by Admin (Salesforce Developers) 
liron169liron169

In general the test for the page should look something like this:

 

Test.setCurrentPageReference(new PageReference('Page.classControllerName'));
ApexPages.StandardController controllerer = new ApexPages.standardController(objID);
classControllerName testClass= new classControllerName(controllerer);

testClass.function1Name();

testClass.function2Name();

All Answers

bob_buzzardbob_buzzard

To be honest I'm surprised you can construct the controller, as you don't have a no-argument constructor for:

 

TicketExtension con = new TicketExtension();   

 Has the test class saved without error?

 

liron169liron169

In general the test for the page should look something like this:

 

Test.setCurrentPageReference(new PageReference('Page.classControllerName'));
ApexPages.StandardController controllerer = new ApexPages.standardController(objID);
classControllerName testClass= new classControllerName(controllerer);

testClass.function1Name();

testClass.function2Name();

This was selected as the best answer
Scott0987Scott0987

liron169 wrote:

In general the test for the page should look something like this:

 

Test.setCurrentPageReference(new PageReference('Page.classControllerName'));
ApexPages.StandardController controllerer = new ApexPages.standardController(objID);
classControllerName testClass= new classControllerName(controllerer);

testClass.function1Name();

testClass.function2Name();


Thank you this was very helpful.   I appreciate the help.