user1298426
user1298426

Reputation: 3717

How can I mock PagedResultList in grails 2.2.4

Seems that PagedResultList is changed in grails 2.2.4 which is causing some issues in unit test.

The constructor is changed from

PagedResultList(list())

to

PagedResultList(GrailsHibernateTemplate template, Criteria crit) 

Upvotes: 1

Views: 661

Answers (2)

Keiji Yamashita
Keiji Yamashita

Reputation: 23

If only care about return values, attach totalCount to ArrayList.

def list = [domainObj1, domainObj2]
list.metaClass.getTotalCount {
    2
}

Upvotes: 2

BhathiyaW
BhathiyaW

Reputation: 356

How about something like this...

def mockC = mockFor(org.hibernate.Criteria) 
mockC.demand.list { return []} //PagedResultList constructor calls this
def pagedList = new PagedResultList(null, mockC.createMock()){
    {
       //Using a static block to set private variables 
       //since we can't call a constructor here!
       list = yourList
       totalCount = yourList.size()
    }
}

Upvotes: 3

Related Questions