IntegrationSpec不能是事务性错误

IntegrationSpec不能是事务性错误

本文介绍了Grails 2.3 IntegrationSpec不能是事务性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近升级到Grails 2.3并尝试将所有旧测试迁移到spock集成测试。但是它在清理时失败,因为我的测试是非事务性的。 Grails doc说测试可以是非事务性的,但我们需要手动处理它,但在这里看起来不太正确。因为我在每次集成测试中都遇到这个错误,因此扩展了IntegrationSpec

  java.lang.IllegalStateException:无法取消激活事务同步 - 不激活
at grails.test.spock.IntegrationSpec.cleanup(IntegrationSpec.groovy:72)

A像这样的简单测试会引发这样的错误:

  import grails.test.spock.IntegrationSpec 

public class DummySpec extends IntegrationSpec {
static transactional = false

def setup(){
}

def cleanup(){
}

def testDummy(){
expect:
1 == 1
}
}


解决方案

我也遇到过这个!很确定它是一个grails bug ...我提交了一个和一个。



引发错误因为在调用 interceptor.destroy() interceptor.isTransactional()

pre $ def $ clean $($)$ b $ perMethodRequestEnvironmentInterceptor?.destroy
perMethodTransactionInterceptor?.destroy ()// break :(


...

私有GrailsTestTransactionInterceptor initTransaction(){
def interceptor = new GrailsTestTransactionInterceptor(applicationContext)
if(interceptor.isTransactional(this))interceptor.init()//还需要destroy()
拦截器
}

我的修正是添加这段代码:

  def cleanup() {
perMethodReque stEnvironmentInterceptor?.destroy()
destroyTransaction(perMethodTransactionInterceptor)
}
$ b $ ...

private void destroyTransaction(GrailsTestTransactionInterceptor拦截器){
if(interceptor?.isTransactional(this))interceptor.destroy()
}

现在解决,你可以创建自己的com.myname.IntegrationSpec修补代码并扩展,而不是grails.test.spock.IntegrationSpec。不理想......但它的工作原理:)

I upgrade to Grails 2.3 recently and try to migrate all old tests to spock integration test. But it fails at cleanup because my test is non-transactional. The Grails doc says test can be non-transactional, but we need to handle it manually, but it seems not quite right here. as I am getting this error in every integration test extending IntegrationSpec

java.lang.IllegalStateException: Cannot deactivate transaction synchronization - not active
    at grails.test.spock.IntegrationSpec.cleanup(IntegrationSpec.groovy:72)

A simple test like this would throw that error:

import grails.test.spock.IntegrationSpec

public class DummySpec extends IntegrationSpec {
   static transactional = false

   def setup() {
   }

   def cleanup() {
   }

   def testDummy() {
      expect:
      1 == 1
   }
}
解决方案

I ran into this too! Pretty sure its a grails bug... I submitted a jira and a patch.

The error is thrown because the code in grails.test.spock.IntegrationSpec does not check for interceptor.isTransactional() before calling 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
}

My fix was to add this code:

def cleanup() {
  perMethodRequestEnvironmentInterceptor?.destroy()
  destroyTransaction(perMethodTransactionInterceptor)
}

...

private void destroyTransaction(GrailsTestTransactionInterceptor interceptor){
  if (interceptor?.isTransactional(this)) interceptor.destroy()
}

To work around for now, you can just create your own com.myname.IntegrationSpec with the patched code and extend that instead of grails.test.spock.IntegrationSpec. Not ideal... but it works :)

这篇关于Grails 2.3 IntegrationSpec不能是事务性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:53