Unit testing Abstract classes in GroovymockForConstraintsTests抽象Groovy類
我問以前的單元測試和嘲弄域類的問題,但我不認爲我是不夠具體。我有一個域類:
package toplevel.domain
abstract class Party {
static hasMany = [roles:PartyRole]
static constraints = {
roles(nullable:true)
dateCreated(display:false)
lastUpdated(display:false)
}
List roles
Date dateCreated
Date lastUpdated
}
這裏是我的單元測試:
import grails.test.*
import toplevel.domain.*
class PartyTests extends GrailsUnitTestCase {
Party party
protected void setUp() {
super.setUp()
party = [:] as Party
mockForConstraintsTests(Party, [party])
}
protected void tearDown() {
super.tearDown()
}
void testNullRolesIsValid() {
party.roles = null
assertTrue "The roles should be nullable", party.validate()
}
}
以下是測試結果: 無法創建類[toplevel.domain.Party]的新實例!
org.codehaus.groovy.grails.exceptions.NewInstanceCreationException: 無法創建類[toplevel.domain.Party]的新實例!在 grails.test.MockUtils.prepareForConstraintsTests(MockUtils.groovy:540) 在grails.test.MockUtils $ prepareForConstraintsTests.call(未知 來源)在 grails.test.GrailsUnitTestCase.mockForConstraintsTests(GrailsUnitTestCase.groovy:111) 在PartyTests.setUp(PartyTests.groovy:9)在 _GrailsTest_groovy $ _run_closure4.doCall(_GrailsTest_groovy:203)在_GrailsTest_groovy $ _run_closure4.call(_GrailsTest_groovy)在_GrailsTest_groovy $ _run_closure2.doCall(_GrailsTest_groovy:147)在_GrailsTest_groovy $ _run_closure1_closure19.doCall(_GrailsTest_groovy :113) at _GrailsTest_groovy $ _run_closure1.doCall(_GrailsTest_groovy:96)at TestApp $ _run_closure1.doCall(TestApp.groovy:66)at gant.Gant $ _dispatch_closure4.doCall(Gant.gr oovy:324)在 gant.Gant $ _dispatch_closure6.doCall(Gant.groovy:334)在 gant.Gant $ _dispatch_closure6.doCall(Gant.groovy)在 gant.Gant.withBuildListeners(Gant.groovy:在 344) gant.Gant.this $ 2 $ withBuildListeners(Gant.groovy)在 gant.Gant $這個$ 2 $ withBuildListeners.callCurrent(來源不明)在 gant.Gant.dispatch(Gant.groovy:334)在 gant.Gant.this $ 2 $ dispatch(Gant.groovy)at gant.Gant.invokeMethod(Gant.groovy)at gant.Gant.processTargets(Gant.groovy:495)at gant.Gant.processTargets(Gant.groovy:480) : java.lang.InstantiationException
我不明白。我創建了該類的一個實例,並將其提供給mockForConstraintsTests方法。我究竟做錯了什麼?