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
JennaBJennaB 

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>

 

 

 

Jon Mountjoy_Jon Mountjoy_

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.

Ankit AroraAnkit Arora

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

JennaBJennaB

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!

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
}

// TEST
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



Session__c s = new Session__c(name='Test Session',Event__c='My Test Meeting',published__c=true,
Session_End_Time__c='14:00:00', Session_Abstract__c='this is the summary',Session_Code__c=600.00, Session_Start_Time__c='13:00');
insert s;
Session__c s1 = new Session__c(name='Test Session2',Event__c='My Test Meeting',published__c=false,
Session_End_Time__c='14:00:00', Session_Abstract__c='this is the summary2',
Session_Code__c=610.00, Session_Start_Time__c='13:00:00' );
insert s1;

Account ac = new Account(name='Accounting Svc');
insert ac;

Contact c = new Contact(firstname='Speaker',lastname='One',Account=ac);
insert c;

Speaker__c p = new Speaker__c(name='SP-999',Contact__c='Speaker One',Session__c='Test Session',
Event__c='My Test Meeting',Web_Speaker__c = 'CONFIRMED');
insert p;
Speaker__c p1 = new Speaker__c(name='SP-998',Contact__c='Speaker One',Session__c='Test Session2',
Event__c='My Test Meeting',Web_Speaker__c = 'NOT CONFIRMED');
insert p1;

test.startTest(); //now start



SessionListingController x = new SessionListingController();

List<WrapperSession> out = x.getMainListSession();
Error: Compile Error: Method does not exist or incorrect signature: [SessionListingController].getMainListSession() at line 79 column 32


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.



}
}

 

 

 


Jon Mountjoy_Jon Mountjoy_

Ah, you've defined mainListSession as a property.

 

So instead of:

List<WrapperSession> out  = x.getMainListSession();

try

List<WrapperSession> out  = x.mainListSession;