unit-testing - Grails 3 集成测试中没有事务管理器错误

标签 unit-testing grails spock grails-3.0 grails-3.1

我创建了一个新的 Grails 3.1.4 angular 项目以及一些扩展 RestfulController 的域对象和 Controller 。 .我在下面创建了集成测试。当我跑 grails test-app -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)

测试类:
package com.waldoware.invoicer

import grails.test.mixin.integration.Integration
import grails.transaction.*
import spock.lang.*

@Integration
@Rollback
class BillingEntityRestControllerIntegrationSpec extends Specification {

    def setupData() {
        def biller = new BillingEntity()
        biller.with {
            companyName = "Acme, Inc."
        }
        def ledger = new Ledger(name: "My Ledger", billingEntity: biller).save(failOnError: true, flush: true)
    }

    void 'test all entities'() {
        when:
        setupData()
        new BillingEntityRestController().index()

        then:
        response.contentType == 'application/json;charset=UTF-8'
        response.status == HttpServletResponse.SC_OK
        response.text == "[{}]"
    }
}

我确实在 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 中没有配置持久性插件,这会有所帮助。设置事务管理器(示例包括 hibernate4mongodbneo4j 等,或者您没有在 dataSource 中配置 grails-app/conf/application.yml

如果是这种情况,只需删除 @Rollback注释,这应该可以解决问题。

关于unit-testing - Grails 3 集成测试中没有事务管理器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36308477/

相关文章:

c# - 如何在 C# 中对算法进行单元测试?

javascript - Angular Translate 单元测试 - 无法注入(inject) $translateProvider

oracle - 有人可以用简单的英语解释 ORA-29861 错误及其可能的原因吗?

java - 为什么 mock 对象的 getter() 返回 null?

unit-testing - 在 Controller 测试期间无法保存

perl - 测试 Web 应用程序的最佳方法是什么,尤其是在并发访问方面?

grails - 不费吹灰之力即可部署应用

javascript - 我怎样才能使grails remoteFunction正常工作?

testing - 在 groovy(Spock) 中测试文件结构

c# - 单元测试隔离异常 : Throws Exception while running Shims test in Release/Debug mode