東桑西移Grails的構建測試數據插件 - 調用DomainObject.build()時MissingMethodException
調用MyDomainObject.build()通過生成測試數據的插件時,我收到以下錯誤在我的單元測試:
異常
groovy.lang.MissingMethodException: No signature of method: us.maponline.pesticide.PesticideProfile.addToApplicators() is applicable for argument types: (us.maponline.pesticide.PesticideApplicator) values: [us.maponline.pesticide.PesticideApplicator : null]
Possible solutions: getApplicators()
at grails.buildtestdata.handler.NullableConstraintHandler.addInstanceToOwningObjectCollection(NullableConstraintHandler.groovy:121)
at grails.buildtestdata.handler.NullableConstraintHandler.populateDomainProperty(NullableConstraintHandler.groovy:88)
at grails.buildtestdata.handler.NullableConstraintHandler.handle(NullableConstraintHandler.groovy:17)
at grails.buildtestdata.DomainInstanceBuilder.createMissingProperty(DomainInstanceBuilder.groovy:187)
at grails.buildtestdata.DomainInstanceBuilder.populateInstance(DomainInstanceBuilder.groovy:147)
at grails.buildtestdata.DomainInstanceBuilder.build(DomainInstanceBuilder.groovy:124)
at grails.buildtestdata.DomainInstanceBuilder.build(DomainInstanceBuilder.groovy:123)
at grails.buildtestdata.BuildTestDataService$_addBuildMethods_closure1.doCall(BuildTestDataService.groovy:25)
at us.maponline.pesticide.PesticideLogServiceTests.testSaveLog(PesticideLogServiceTests.groovy:20)
| Completed 1 unit test, 1 failed in 5289ms
每堆棧跟蹤,這是buildtestdata插件代碼內發生。看起來,我的類PesticideApplicator在添加到PesticideProfile類時是空的。
當我要傳遞給PesticideProfile時,我要求構建的類是否爲null,這怎麼可能?
源代碼
測試用例
@TestFor(PesticideLogService)
@Build([PesticideLog,PesticideApplicator,PesticideProfile])
class PesticideLogServiceTests
{
void testSaveLog() {
PesticideApplicator applicator = PesticideApplicator.build()
def result = service.createLog(applicator, new Date())
result.errors.each {
log.info "got an error. field = $it.field, message:$it.defaultMessage, rejected value = $it.rejectedValue "
}
assert result: 'no result returned'
assert result.success: 'save failed'
assert result.result instanceof PesticideLog: "result was not PesticideLog"
assert applicator.user.pesticideLogs.size() > 0 : 'expected at least on log to be created.'
}
}
PesticideProfileLog
class PesticideProfile
{
def User user
String companyName
static constraints = {
}
static belongsTo = User
static hasMany = [sites: PesticideSite, applicators: PesticideApplicator]
}
PesticideApplicator
class PesticideApplicator
{
String firstName
String lastName
String company
PesticideApplicatorLicense licenseType
Phone phoneNumber
static belongsTo = [profile:PesticideProfile]
static constraints = {
company blank: false, maxSize: 55
firstName blank: false, maxSize: 55
lastName blank: false, maxSize: 100
phoneNumber nullable: true
}
static mapping = {
licenseType length: 55
}
def getUser(){
profile?.user
}
}
感謝您的幫助!
如果它不是一個真正的字段持久化到數據庫,但你希望它仍然在Grails域對象中有bean get/set語義,你應該將該字段添加到'static transient'列表中:http:// grails.org/doc/latest/ref/Domain%20Classes/transients.html BuildTestData(和GORM)將會知道忽略它。我不熟悉Grails的@ Transient註解(這是一個休眠事情,而不是我認爲的GORM事情),而grails頁面沒有提及它。 Grails文檔中還有其他地方嗎? –
感謝您的信息,特德。你是對的,「靜態瞬態」屬性起作用。我也發現了另一個解決方案(查看spring安全插件生成的代碼):'transient String getUser(){profile?.user}'。要麼工作得很好。 –