You need to sign in to do that
Don't have an account?
bohemianguy100
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.
Hi
Where are you calculating page number i.e. initializing, incrementing on next etc..
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.
if you can share VF then i can think abt some help..
here is my VF code: