【问题标题】:How can I mock PagedResultList in grails 2.2.4如何在 grails 2.2.4 中模拟 PagedResultList
【发布时间】:2013-09-20 05:06:31
【问题描述】:

似乎 PagedResultList 在 grails 2.2.4 中发生了更改,这导致了单元测试中的一些问题。

构造函数改自

PagedResultList(list())

PagedResultList(GrailsHibernateTemplate template, Criteria crit) 

【问题讨论】:

    标签: grails groovy mocking hibernate-criteria


    【解决方案1】:

    这样的事情怎么样...

    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()
        }
    }
    

    【讨论】:

    • 完美。您能否详细说明 //使用静态块设置私有变量 //因为我们不能在这里调用构造函数!。您是否还故意使用了 2 个括号 {{?
    • 抱歉回复晚了 - 最后一个 } 不见了 - 现在更新了。静态块,因为你不能调用内部类的构造函数。
    【解决方案2】:

    如果只关心返回值,请将totalCount 附加到ArrayList

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

    【讨论】:

      猜你喜欢
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 2011-10-09
      相关资源
      最近更新 更多