【问题标题】:No transactionManager error in Grails 3 Integration testGrails 3 集成测试中没有 transactionManager 错误
【发布时间】:2016-07-18 10:27:21
【问题描述】:

我创建了一个新的 Grails 3.1.4 angular 项目以及一些扩展 RestfulController 的域对象和控制器。我在下面创建了集成测试。当我运行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

【问题讨论】:

  • 我想要集成测试的唯一原因是我有一个自定义 JSON Marshaller,它不会在单元测试中调用。
  • 你有没有想过这个问题?我正在迁移 Grails 2 应用程序,并在我们的一些集成测试中看到了同样的问题。
  • @rado 我刚刚删除了自定义编组器以支持 gson。这是 Grails 3 的发展方向:-)

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


【解决方案1】:

如果您没有在 build.gradle 中配置用于设置事务管理器的持久性插件(示例包括 hibernate4mongodbneo4j 等,或者您没有 @987654325 @在grails-app/conf/application.yml中配置。

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

【讨论】:

  • 我想保留@Rollback,以便测试运行不会污染数据库以进行其他测试。我确实尝试删除回滚,但在运行测试时出现此错误:org.springframework.dao.DataAccessResourceFailureException: Could not obtain current Hibernate Session; nested exception is org.hibernate.HibernateException: No Session found for current thread
【解决方案2】:

在对我自己的集成测试进行故障排除时遇到了这个问题。我通过删除 out 目录解决了我的问题。

delete project-folder/out/

现在您还需要清理和重建一个 war 文件。这必须在构建过程中运行一些额外的步骤并解决一些问题

./grailsw clean
./grailsw war

现在,当您运行测试时,您应该不会看到错误消息。

【讨论】:

    猜你喜欢
    • 2012-05-04
    • 1970-01-01
    • 2018-11-12
    • 2013-10-19
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多