我最近升级到 Grails 2.3 并尝试将所有旧测试迁移到 spock 集成测试。但它在清理时失败了,因为我的测试是非事务性的。 Grails 文档说测试可以是非事务性的,但我们需要手动处理它,但在这里似乎不太正确。因为我在扩展 IntegrationSpec 的每个集成测试中都会遇到此错误
java.lang.IllegalStateException: Cannot deactivate transaction synchronization - not active
at grails.test.spock.IntegrationSpec.cleanup(IntegrationSpec.groovy:72)
像这样的简单测试会抛出该错误:
import grails.test.spock.IntegrationSpec
public class DummySpec extends IntegrationSpec {
static transactional = false
def setup() {
}
def cleanup() {
}
def testDummy() {
expect:
1 == 1
}
}
我也遇到了这个!很确定这是一个 grails bug...我提交了一个jira http://jira.grails.org/browse/GRAILS-10904 and a patch https://github.com/grails/grails-core/pull/436.
抛出错误是因为 grails.test.spock.IntegrationSpec 中的代码未检查interceptor.isTransactional()
打电话之前interceptor.destroy()
def cleanup() {
perMethodRequestEnvironmentInterceptor?.destroy()
perMethodTransactionInterceptor?.destroy() //breaks :(
}
...
private GrailsTestTransactionInterceptor initTransaction() {
def interceptor = new GrailsTestTransactionInterceptor(applicationContext)
if (interceptor.isTransactional(this)) interceptor.init() //also need for destroy()
interceptor
}
我的解决方法是添加以下代码:
def cleanup() {
perMethodRequestEnvironmentInterceptor?.destroy()
destroyTransaction(perMethodTransactionInterceptor)
}
...
private void destroyTransaction(GrailsTestTransactionInterceptor interceptor){
if (interceptor?.isTransactional(this)) interceptor.destroy()
}
现在要解决这个问题,您只需使用修补后的代码创建您自己的 com.myname.IntegrationSpec 并扩展它,而不是 grails.test.spock.IntegrationSpec。不理想......但它有效:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)