簡單地說,我有以下情形:測試afterInsert Grails中
class OwnedRights {
static belongsTo = [comp: Competition]
@Transactional
def afterInsert() {
// Erroring out here.
Event.findAllByComp(comp).each { event ->
// Do something with event.
}
}
}
當我試圖在我的單元測試保存的東西,如下列:
def ownedRights = new OwnedRights(params).save(flush: true, failOnError: true)
我看到下面的堆棧跟蹤:
java.lang.NullPointerException at org.springframework.transaction.support.TransactionTem (org.codehaus.groovy.grails.orm.support.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:85) at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java: 209) 在org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:194)
這導致我this Jira Issue這表明我沒有在我的測試使用@Mock
註釋,但是,我嘲笑所有使用的域類:
@Mock([OwnedRights, Sport, Competition, Event])
這是一個包含GORM邏輯的hibernate事件的已知問題嗎?
試圖解決使用的metaClass
我試圖重寫afterInsert()
和beforeDelete
方法:
OwnedRights.metaClass.afterInsert = null;
OwnedRights.metaClass.beforeDelete = null;
和
OwnedRights.metaClass.afterInsert = {};
OwnedRights.metaClass.beforeDelete = {};
這兩者上都沒有效果結果。如果我註釋掉afterInsert事件,則保存完美。
我刪除了@Transactional註解。最後,我完全刪除了這些事件並做了一些重構。 – christopher
那麼,沒有更多afterInsert?...無賴。我試圖在afterInsert中測試代碼,並且無法使其工作。 – Philippe