简单地说,我有以下情形:测试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