You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Grails 3.3.1集成测试运行时抛出IllegalStateException求助

解决Grails 3.3.1集成测试No GORM implementations configured异常

这个问题我之前排查过类似的,结合你的测试代码和报错信息来看,核心原因是**@Rollback注解触发了GORM事务管理器的初始化,但你的测试既不需要数据库操作,测试环境也没有正确配置GORM实现**。下面给你几个针对性的解决办法:

1. 最快速的修复:移除@Rollback注解

你的测试只是Mock了EmailService的方法,完全没有涉及任何数据库操作,@Rollback注解原本是用来在测试结束后回滚数据库变更的,这里根本用不上。去掉这个注解后,测试就不会触发GORM的初始化流程,自然不会抛出这个异常。修改后的测试代码如下:

import grails.testing.mixin.integration.Integration
import org.springframework.beans.factory.annotation.Autowired
import spock.lang.Specification

@Integration
class EmailSpec extends Specification {
    @Autowired EmailService service

    def 'test send email'() {
        when: 'email gets sent'
        def sendMailCalled = false
        service.metaClass.sendTestEMail = { sendMailCalled = true }
        service.sendTestEMail("test@myprovider.de")
        then:
        sendMailCalled == true
    }
}

2. 如果项目其他测试需要GORM,检查依赖与配置

如果你的项目本身需要GORM支持(比如其他测试涉及数据库操作),那需要确保测试环境的GORM配置完整:

  • 检查build.gradle中的依赖,确保包含GORM实现(以Hibernate5为例):
    compile "org.grails.plugins:hibernate5"
    testCompile "org.grails:grails-datastore-test-support"
    
  • 检查测试环境的数据源配置(grails-app/conf/application-test.yml),配置一个内存数据库用于测试:
    dataSource:
        url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
        driverClassName: org.h2.Driver
        username: sa
        password:
    

3. 排查配置变更痕迹

你提到测试之前正常运行,那大概率是近期的配置变更导致的:

  • 有没有误删build.gradle中的GORM相关依赖?
  • 有没有修改application-test.yml中的数据源配置,导致GORM无法初始化?
  • 有没有调整过测试类的注解组合,比如新增了触发GORM初始化的注解?

内容的提问来源于stack exchange,提问作者Erando

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:04:49