4

我最近升级到 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
   }
}
4

2 回答 2

3

我也遇到了这个!很确定它是一个 grails 错误......我提交了一个jira和一个补丁

interceptor.isTransactional()抛出错误是因为 grails.test.spock.IntegrationSpec 中的代码在调用之前没有检查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。不理想......但它有效:)

于 2013-12-10T19:59:43.073 回答
2

Grails 2.3 默认随 Spock 一起提供。只需删除您自己定义的 spock 依赖项,确保导入 grails.test.spock.IntegrationSpec 并且它应该可以工作。

于 2013-12-20T17:24:50.777 回答