Unit testing Grails3集成测试中没有transactionManager错误
我创建了一个新的Grails 3.1.4 angular项目,以及一些扩展了Unit testing Grails3集成测试中没有transactionManager错误,unit-testing,grails,spock,grails-3.0,grails-3.1,Unit Testing,Grails,Spock,Grails 3.0,Grails 3.1,我创建了一个新的Grails 3.1.4 angular项目,以及一些扩展了RestfulController的域对象和控制器。我已经创建了下面的集成测试。当我运行grails测试应用程序-integration时,我得到了一个错误 java.lang.IllegalStateException: No transactionManager was specified. Using @Transactional or @Rollback requires a valid configured t
RestfulController
的域对象和控制器。我已经创建了下面的集成测试。当我运行grails测试应用程序-integration
时,我得到了一个错误
java.lang.IllegalStateException: No transactionManager was specified. Using @Transactional or @Rollback requires a valid configured transaction manager. If you are running in a unit test ensure the test has been properly configured and that you run the test suite not an individual test method.
at grails.transaction.GrailsTransactionTemplate.<init>(GrailsTransactionTemplate.groovy:60)
at com.waldoware.invoicer.BillingEntityRestControllerIntegrationSpec.$tt__$spock_feature_0_0(BillingEntityRestControllerIntegrationSpec.groovy:29)
at com.waldoware.invoicer.BillingEntityRestControllerIntegrationSpec.test all entities_closure2(BillingEntityRestControllerIntegrationSpec.groovy)
at groovy.lang.Closure.call(Closure.java:426)
at groovy.lang.Closure.call(Closure.java:442)
at grails.transaction.GrailsTransactionTemplate$1.doInTransaction(GrailsTransactionTemplate.groovy:70)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at grails.transaction.GrailsTransactionTemplate.executeAndRollback(GrailsTransactionTemplate.groovy:67)
at com.waldoware.invoicer.BillingEntityRestControllerIntegrationSpec.test all entities(BillingEntityRestControllerIntegrationSpec.groovy)
我确实在application.yml中设置了一个数据源:
environments:
development:
dataSource:
dbCreate: none
url: jdbc:h2:./devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
test:
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
production:
dataSource:
dbCreate: update
url: jdbc:h2:./prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
properties:
jmxEnabled: true
initialSize: 5
maxActive: 50
minIdle: 5
maxIdle: 25
maxWait: 10000
maxAge: 600000
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 60000
validationQuery: SELECT 1
validationQueryTimeout: 3
validationInterval: 15000
testOnBorrow: true
testWhileIdle: true
testOnReturn: false
jdbcInterceptors: ConnectionState
defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED
如果您的build.gradle
中没有配置用于设置事务管理器的持久性插件(示例包括hibernate4
、mongodb
、neo4j
等),或者您没有在grails-app/conf/application.yml
中配置dataSource
,这会有所帮助
如果是这种情况,只需删除@Rollback
注释即可解决问题。在对自己的集成测试进行故障排除时遇到此问题。我通过删除out目录解决了问题
delete project-folder/out/
现在您还需要清理和重建war文件。这必须在构建过程中运行一些额外的步骤,并解决一些问题
./grailsw clean
./grailsw war
现在,当您运行测试时,您不应该看到错误消息。我希望保留@Rollback
,以便测试运行不会污染其他测试的数据库。我确实尝试删除回滚,但在运行测试时出现此错误:org.springframework.dao.DataAccessResourceFailureException:无法获取cur租用Hibernate会话;嵌套异常为org.Hibernate.HibernateException:未找到当前线程的会话
我希望进行集成测试的唯一原因是我有一个自定义JSON封送器,该封送器未在单元测试下调用。你有没有发现这个问题?我正在迁移一个Grails 2应用程序,并在其中一些应用程序中发现了相同的问题我们的集成测试。@rado我刚刚删除了定制封送拆收器,取而代之的是gson。这是Grails的3条路:-)
./grailsw clean
./grailsw war