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
bohemianguy100bohemianguy100 

system assertEquals unit test error for paing code

I have implemented the standard list paging functionality and I've written my unit tests to cover the controller code.  My code is covered 100%, but I'm getting two errors on my asserts.

 

Here is my controller class:

 

public class BariatricEventsController {
	
	public ApexPages.StandardSetController setCtrl{get; set;}
  	
  	public BariatricEventsController()
 	{
   		setCtrl = new ApexPages.StandardSetController(Database.getQueryLocator([Select Name, Event_Date__c, Time_of_Event__c,Type__c,Service_Line__c, openSlots__c, SFDC_Facility__r.Name,Fee__c,Remaining_Space_Available__c,
                Maximum_Space_Available__c, Event_Status__c from SFDC_Event__c where (Event_Status__c ='Open' or Event_Status__c='Wait List' or 
                Event_Status__c='Closed') and Service_Line__c LIKE '%bariatric%' and Event_Date__c >= :System.today() and Event_Date__c <= :System.today().addDays(30) order by Event_Date__c]));
 		setCtrl.setPageSize(10);
 	}
  	
  	public Boolean hasNext {
  		get {
  			return setCtrl.getHasNext();
  		}
  	}
  	
  	public Boolean hasPrevious {
		get {
			return setCtrl.getHasPrevious();
		}
		set;
	}
  	
  	public Integer pageNumber {
		get {
			return setCtrl.getPageNumber();
		}
		set;
	}
	
	public Integer totalPages {
		get {
			Decimal totalPages = Decimal.valueOf(setCtrl.getPageNumber()).round(RoundingMode.CEILING);
			return totalPages.intValue();
		}
		set;
	}
  	
 	public void first() {
 		setCtrl.first();
 	}

 	public void last() {
 		setCtrl.last();
 	}

 	public void previous() {
 		setCtrl.previous();
 	}

 	public void next() {
 		setCtrl.next();
 	}
  	
  	
  	public List<SFDC_Event__c> getEvents()
  	{
    	return (List<SFDC_Event__c>)setCtrl.getRecords();
  	}

}

 Here is my unit test class:

 

@isTest
private class TestBariatricEventsController {

	private static testMethod void test() {
		
		SFDC_Facility__c sfdcfacility = new SFDC_Facility__c(name='Test Facility');
        insert sfdcfacility;
        
        Campaign camp = new Campaign(Name='Test',isActive=True, Approved__c=True, Type='Advertisement',Status='Planned',StartDate=Date.Today());
        insert camp;
		
		SFDC_Event__c sfdcevent = new SFDC_Event__c();
		sfdcevent.name='Test1';
		sfdcevent.Registration_Fee__c=0;
		sfdcevent.Service_Line__c='Bariatric Weight Loss Surgery';
		sfdcevent.type__c='Seminar';
		sfdcevent.event_status__c='Open';
		sfdcevent.Maximum_Space_Available__c=12;
		sfdcevent.Event_Date__c=System.today().addDays(15);
		sfdcevent.SFDC_Facility__c=sfdcfacility.Id;
		sfdcevent.Primary_Campaign__c=camp.Id;
		
		SFDC_Event__c sfdcevent2 = new SFDC_Event__c();
		sfdcevent2.name='Test2';
		sfdcevent2.Registration_Fee__c=0;
		sfdcevent2.Service_Line__c='Bariatric Weight Loss Surgery';
		sfdcevent2.type__c='Seminar';
		sfdcevent2.event_status__c='Open';
		sfdcevent2.Maximum_Space_Available__c=12;
		sfdcevent2.Event_Date__c=System.today().addDays(15);
		sfdcevent2.SFDC_Facility__c=sfdcfacility.Id;
		sfdcevent2.Primary_Campaign__c=camp.Id;
        
        List<SFDC_Event__c> evs = new List<SFDC_Event__c>();
		evs.add(sfdcevent);
		evs.add(sfdcevent2);
		insert evs;
        
        PageReference pageRef = Page.BariatricEvents;
		Test.setCurrentPage(pageRef);
		
		ApexPages.StandardSetController ssc = new ApexPages.Standardsetcontroller(evs);
		BariatricEventsController ctrl = new BariatricEventsController();
		ctrl.first();
		ctrl.getEvents();
		ctrl.last();
		ctrl.next();
		ctrl.previous();
		
		system.assertEquals(true, ctrl.hasNext);
		system.assertEquals(true, ctrl.hasPrevious);
		system.assertEquals(3, ctrl.pageNumber);
		system.assertEquals(3, ctrl.totalPages);
		
	}

}

 The assers fail on the following two lines:

 

system.assertEquals(1, ctrl.pageNumber);
system.assertEquals(1, ctrl.totalPages);

 The assers expect the value of 3 instead of 1?  But, my page size is set to 10 in my controller and I've only got two records in my list, so shouldn't the pageNumber and totalPages equal 1?

 

What am I missing?

 

Thanks for any help.

Ajay.DixitAjay.Dixit

Hi

 

Where are you calculating page number i.e. initializing, incrementing on next etc..

bohemianguy100bohemianguy100

The page size is set to 10 in the controller, so in my test if I only have two records shouldn't the page number property and total pages by default be 1?  Can you provide an example of what you mean by initializing, incrementing on next and calculating the page number?

 

Thanks.

Ajay.DixitAjay.Dixit

if you can share VF then i can think abt some help..

bohemianguy100bohemianguy100

here is my VF code:

 

<apex:page controller="BariatricEventsController" showHeader="false" standardStylesheets="false" >
	<style>
        .errorMsg { color: red; font-weight: strong;}
        .even{border:10px red; border-style: dotted double;background:transparent url({!URLFOR($Resource.finddoctor, 'images/gradient-alt-row.gif')}) repeat;height:60px;}
        .odd{height:60px;}
        #main{overflow: hidden; scrollbar: no; width: 100%; frameBorder=0; height: 100%;}
  	</style>
  	<apex:stylesheet value="{!URLFOR($Resource.event,'css/shc.css')}"/>
  	<apex:stylesheet value="{!URLFOR($Resource.event,'css/reset.css')}"/>
  	<apex:stylesheet value="{!URLFOR($Resource.event,'css/events.css')}"/>
  	<apex:stylesheet value="{!URLFOR($Resource.event,'css/superfish.css')}"/>
  	<apex:stylesheet value="{!URLFOR($Resource.event,'css/ui.selectmenu.css')}"/>
  	<apex:stylesheet value="{!URLFOR($Resource.event,'css/custom-theme/jquery-ui-1.7.2.custom.css')}"/>
  	<apex:includeScript value="{!URLFOR($Resource.event,'scripts/ui.selectmenu.js')}"/>
  	<apex:includeScript value="{!URLFOR($Resource.event,'scripts/jquery.js')}"/>
  	<apex:includeScript value="{!URLFOR($Resource.event,'scripts/jquery-ui.js')}"/>
  	<apex:includeScript value="{!URLFOR($Resource.event,'scripts/superfish.js')}"/>
	<body style="background: #FFFFFF;">
		<div id="main">
			<div>
				<apex:form >
					<div id="article-no-padding">
						<div id="events-list-meta">
            				<div id="events-list-title"><apex:outputLabel Value="Upcoming Bariatric Events" /> </div>
            				<div id="events-list-description"><apex:outputLabel Value="Bariatric Events scheduled in the next 30 days" style="font-family:Arial,Helvetica,sans-serif;font-size:12px;"/>  </div>          
            				<br/><br/>
        				</div>
						<apex:dataTable id="EventListTable" value="{!Events}" var="evs" columns="8" styleClass="events-list" cellspacing="0" cellpadding="0" border="0" rowClasses="odd,even">
       						<apex:column style="border-bottom:1px solid #CCCCCC;font-family:Arial,Hevsvetica,sans-serif;font-size:12px;" width="250px" headerValue="Date/Time" styleClass="events-date">
            					<br/><apex:outputText /><br/><br/>
                				<p><apex:outputText value="{!evs.Name}" styleClass="event-type"/></p>
                				<br/>
								<p><apex:outputText value="{!evs.Event_Date__c} {!evs.Time_of_Event__c}" styleClass="event-date" style="padding-left:10px;"/></p>      
							</apex:column><br/>
            				<apex:column style="border-bottom:1px solid #CCCCCC;font-family:Arial,Hevsvetica,sans-serif;font-size:12px;" width="80px" headerValue="Type" styleClass="events-type">
                				<br/><apex:outputText /><br/><br/>
                				<p><apex:outputText value="{!evs.Type__c}" styleClass="event-type"/></p>
            				</apex:column>
            				<apex:column style="border-bottom:1px solid #CCCCCC;font-family:Arial,Hevsvetica,sans-serif;font-size:12px;" width="180px" headerValue="Service Line" styleClass="events-line">
            					<br/><apex:outputText /><br/><br/>
            					<p><apex:outputText value="{!evs.Service_Line__c}" styleClass="event-line"/></p>
           					</apex:column>
							<apex:column style="border-bottom:1px solid #CCCCCC;font-family:Arial,Hevsvetica,sans-serif;font-size:12px;" width="120px" headerValue="Facility" styleClass="events-Facility">
            					<br/><apex:outputText /><br/><br/>
            					<p><apex:outputText value="{!evs.SFDC_Facility__r.Name}" styleClass="event-facility"/></p>
          					</apex:column>
          					<apex:column style="border-bottom:1px solid #CCCCCC;font-family:Arial,Hevsvetica,sans-serif;font-size:12px;" width="60px" headerValue="Fee" styleClass="events-Fee">
            					<br/><apex:outputText /><br/><br/>
              					<p><apex:outputText value="{!evs.Fee__c}" styleClass="event-fee"/></p>
          					</apex:column>
          					<apex:column style="border-bottom:1px solid #CCCCCC;font-family:Arial,Hevsvetica,sans-serif;font-size:12px;" width="55px" headerValue="Status" styleClass="events-status">
            					<br/><apex:outputText /><br/><br/>
            					<p><apex:outputText value="{!evs.Event_Status__c}" styleClass="event-status"/></p>
        					</apex:column>
        					<apex:column style="border-bottom:1px solid #CCCCCC;font-family:Arial,Hevsvetica,sans-serif;font-size:12px;" width="55px" headerValue="Register" styleClass="events-status" >
        						<br/><apex:outputText /><br/><br/>
								<apex:outputLink value="/event/eventRegistrationBWL" ><span class="event-status" >Register</span>
									<apex:param name="EventID" value="{!evs.Id}"/>
								</apex:outputLink>
							</apex:column>
   						</apex:dataTable>
   						<div id="page-controls-events-main" >
   							<apex:commandButton id="first" value="First" action="{!first}" style="-moz-background-clip:border;-moz-background-inline-policy:continuous;-moz-background-origin:padding;background:#660066 url({!URLFOR($Resource.shcare, 'images/arrow-white-right.gif')}) no-repeat scroll 90% 50%;border:1px solid #CCCCCC;color:white;float:left;margin:0 39px 0 0;padding:5px 12px 5px 12px;text-decoration:none;width:70px;" />
                        	<apex:commandButton id="prev" value="Prev" action="{!previous}" rendered="{!hasPrevious}" style="-moz-background-clip:border;-moz-background-inline-policy:continuous;-moz-background-origin:padding;background:#660066 url({!URLFOR($Resource.shcare, 'images/arrow-white-right.gif')}) no-repeat scroll 90% 50%;border:1px solid #CCCCCC;color:white;float:left;margin:0 39px 0 0;padding:5px 12px 5px 12px;text-decoration:none;width:70px;" />  
                       		<apex:commandButton id="last" value="Last" action="{!last}" style="margin-right:70px;-moz-background-clip:border;-moz-background-inline-policy:continuous;-moz-background-origin:padding;background:#660066 url({!URLFOR($Resource.shcare, 'images/arrow-white-right.gif')}) no-repeat scroll 90% 50%;border:1px solid #CCCCCC;color:white;float:right;margin:0 50px 0 0;padding:5px 12px 5px 12px;text-decoration:none;width:70px;"/>
                       		<apex:commandButton id="next" value="Next" action="{!next}" rendered="{!hasNext}" style="margin-right:70px;-moz-background-clip:border;-moz-background-inline-policy:continuous;-moz-background-origin:padding;background:#660066 url({!URLFOR($Resource.shcare, 'images/arrow-white-right.gif')}) no-repeat scroll 90% 50%;border:1px solid #CCCCCC;color:white;float:right;margin:0 50px 0 0;padding:5px 12px 5px 12px;text-decoration:none;width:70px;"/>
                        	<div class="clear"/>
                        </div>
   					</div>
				</apex:form>
			</div>
		</div>
	</body>
</apex:page>