从1.3.7升级到2.4.4后,Grails单元测试用例无效

时间:2017-11-21 06:05:53

标签: unit-testing grails

我已将Grails从1.3.7迁移到2.4.4版。现在,当我运行单元测试用例时,它失败了,原因如下。有45个以上的测试用例失败,但有以下异常:

测试错误:

  

testEdit(AddressControllerTest):没有方法签名:static entity.Address.get()适用于参数类型:(null)values:[null]

我的班级名称是AddressController.groovy

def edit = {
    def quoteInstance = Quote.get( params.id )

    if(!quoteInstance) {
        flash.message = "Quote not found with id ${params.id}"
        redirect(action:list)
    }
    else {
        return [ quoteInstance : quoteInstance ]
    }
}

这是之前的方法:

@Before
public void setUp() {
    super.setUp()

    flash = [ : ]
    AddressController.metaClass.getFlash= { -> flash }

    params = [ : ]
    AddressController.metaClass.getParams = { -> params }

    redirectParams = [ : ]
    AddressController.metaClass.'static'.redirect = { Map args ->
    redirectParams = args }

    renderParams= [ : ]
    AddressController.metaClass.render = { Map args -> renderParams = args }
}

实际的测试用例是:

@Test
public void testEdit(){

    params['id'] = 123 ;

    Quote.metaClass.static.get = { int id ->
        assertEquals params.id, id
        return null
    }

    QuoteController cc = new QuoteController()
    cc.edit()

    assertEquals 'Quote not found with id 123', flash.message
    assertEquals cc.list, redirectParams.action
}

我怀疑GUnit jar类可能已经改变了。

0 个答案:

没有答案