【问题标题】:Grails...Mocking criteria that returns PagedResultListGrails...返回 PagedResultList 的模拟标准
【发布时间】:2012-07-28 09:40:31
【问题描述】:

在我的控制器中,我有一个操作,它使用标准来命中 db 并获取结果。

params.max = Math.min(params.max ? params.int('max') : 20, 100)    
def c = DomainObj.createCriteria()
    def result =[]
    result = c.list(params) {
        'eq'("employerid", id)
        }

我以这种方式在我的测试用例中模拟了这个调用:

def result=[DomainObj1]         
            def mycriteria =[
                list: {Object params=null,Closure cls -> result}                    
                ]

DomainObj.metaClass.static.createCriteria = {mycriteria}

到目前为止工作正常。

但在控制器中,有一行代码显示 result.totalCount,其中 result 是条件查询的输出,属于 PagedResultList 类型。 但是在测试用例中,我将结果模拟为arrayList,而不是PagedResultList。因此,如果从测试用例运行,代码会在 result.totalCount 处中断。

关于如何模拟对 PagedResultList 而不是 arraylist 的标准响应的任何想法,以便它具有 totalCount

【问题讨论】:

    标签: java hibernate grails junit criteria


    【解决方案1】:

    看看its interface。你可以像

    result = new PagedResultList(list: inctanceList, totalCount: inctanceList.size())
    

    【讨论】:

    • [list: inctanceList, totalCount: inctanceList.size()] as PagedResultList 呢?
    • 不能强制转换,构造函数在2.x中已经完全改变了。
    【解决方案2】:

    对于 Grails3,您可以使用 Groovy 的 tap 闭包:

    PagedResultList<Foo> list = new PagedResultList<Foo>(null).tap {
        totalCount = 23
        resultList = [ foo1, foo2, foo3 ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-16
      • 1970-01-01
      • 2012-07-28
      相关资源
      最近更新 更多