Grails 3集成测试中没有transactionManager错误

时间:2016-03-30 12:10:23

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

我创建了一个新的Grails 3.1.4角度项目以及一些扩展HttpStatus的域对象和控制器。我在下面创建了集成测试。当我运行org.springframework.http时,我收到了错误

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 == "[{}]"
    }
}

2 个答案:

答案 0 :(得分:3)

如果您的build.gradle中没有配置设置事务管理器的持久性插件,则可以提供帮助(示例包括hibernate4mongodbneo4j等。或者您没有在dataSource中配置grails-app/conf/application.yml

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

答案 1 :(得分:0)

在对我自己的集成测试进行故障排除的同时,进行检查。我通过删除out目录解决了我的问题。

delete project-folder/out/

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

./grailsw clean
./grailsw war

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