You need to sign in to do that
Don't have an account?

Custom controller - what to test when page is just displaying data?
It seems like this should be simple, but...
I have created a custom controller that selects sessions and their related speakers. I have a visualforce page that displays this information. All is working fine, but I need to get it moved to production. I've been reading about testing and searching the discussions but I can't get what exactly I need to test for, since my controller doesn't do anything, as far as writing any records, there are no buttons or input accepted on the visualforce page, nothing is passed in the url, it simply displays a list of sessions and speakers. I really need to get a test written so I can get this moved out of sandbox - can anyone help? Any suggestions are appreciated! Thank you.
Below is the controller and page.
public class SessionListingController { public String pitem { get; set; } Public List<WrapperSession> mainListSession {get;set;} Public List<Speaker__c> lstSpeaker {get;set;} public SessionListingController() { mainListSession = new List<WrapperSession>(); for (Session__c a : [SELECT id, name,Session_End_Time__c, Session_Abstract__c, Session_Code__c, Session_Start_Time__c, Session_Start_Date_Time__c, (SELECT Name, NameTitle_Org_St__c, Web_Speaker__c, Speaker__c.Contact__r.Photo__c FROM speaker__r WHERE Web_Speaker__c = 'CONFIRMED' ORDER BY Speaker_Order__c) FROM session__c WHERE Session__c.Event__r.Name = 'My Test Meeting' And Published__c = True ORDER BY Session_Start_Date_Time__c, Session_Code__c]) { WrapperSession wrapSess = new WrapperSession(); wrapSess.sess = a; Speaker__c[] speakers = a.speaker__r; lstSpeaker = new List<Speaker__c>(); For(Speaker__c speaker : speakers) { lstSpeaker.add(speaker); } wrapSess.speaker = lstSpeaker; mainListSession.Add(wrapSess); } } public class WrapperSession // this is the wrapper class { public Session__c sess{get;set;} //to capture the Session public List<Speaker__c> speaker {get;set;} // to capture the related Speaker } }
VF page:
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" Controller="SessionListingController" cache="false"> <apex:outputText value="Meeting Sessions" /> <apex:dataTable value="{!mainListSession}" var="lw" > <apex:column headerValue=" "> <br></br> <apex:outputField value="{!lw.sess.Session_Start_Time__c}"/><br></br> <strong><apex:outputField value="{!lw.sess.Name}" /></strong> <br></br><apex:outputField value="{!lw.sess.Session_Abstract__c}" /> <apex:outputPanel rendered="{!lw.sess.Session_Abstract__c!=''}"> <br /></apex:outputPanel> <apex:dataTable value="{!lw.speaker}" var="lwNote"> <apex:column headerValue=" " > <apex:outputField value="{!lwNote.NameTitle_Org_St__c}" /> </apex:column> </apex:dataTable> </apex:column> </apex:dataTable> </apex:page>
Hi JennaB, and welcome to the boards,
Well, note that your page may just be displaying data, but you're actually executing business logic to find that data.
In particular you're quering for a list of sessions, you expect only speakers that are marked as 'CONFIRMED' to display, and so on.
So it sounds like you want a test method that does something like:
static testMethod void myTest() {
// perform some data preparation
// insert some test speaker data, some of which are CONFIRMED and some of which are not
// insert some test session data, some of which as published__c==true, and some not
test.startTest(); //now start
SessionListingController x = new SessionListingController();
List<WrapperSession> out = x.getMainListSession();
test.stopTest();
// at this point you should now test that "out" has the expected records
// for example, ensure that all your test session data is in out that you expect, and that the ones that don't have "publshed==true' are not
// and ensure that only the expected speakers are related to those sessions.
}
Something like that! Check out this article, which explains the basic frameworks for tests like this.
Hi JennaB,
In addition to Jon reply, just one thing you can also put asserts with in the test class to test wheather the test data you have inserted is being qureid or not (check list size) as this data is going to be displayed on UI.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Thank you for the suggestions. I've copied your code and started adding some data for the test but there is an error relating to the line List<WrapperSession> out = x.getMainListSession();
"Error: Compile Error: Method does not exist or incorrect signature: [SessionListingController].getMainListSession() at line 79 column 32"
Do you know that would refer to? Thank you!
Ah, you've defined mainListSession as a property.
So instead of:
try