Grails 2.3 IntegrationSpec Cannot Be Transactional False

I recently upgraded to Grails 2.3 and try to transfer all the old tests to the integration integration test. But it does not work during cleanup, because my test is not transactional. The Grails doc document says that the test may not be transactional, but we have to process it manually, but that doesn't seem entirely correct. as I get 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 will result in an error:

 import grails.test.spock.IntegrationSpec public class DummySpec extends IntegrationSpec { static transactional = false def setup() { } def cleanup() { } def testDummy() { expect: 1 == 1 } } 
+3
source share
2 answers

I also came across this! Pretty sure this is grails bug ... I sent jira and the patch .

The error is caused because the code in grails.test.spock.IntegrationSpec does not check 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 get around this now, you can simply create your own com.myname.IntegrationSpec with the corrected code and extend it instead of grails.test.spock.IntegrationSpec. Not perfect ... but it works :)

+2
source

Grails 2.3 ships by default with Spock. Just remove your own specific spock dependency to import the grails.test.spock.IntegrationSpec file and it should work.

+2
source

Source: https://habr.com/ru/post/1403391/


All Articles