我的应用程序使用 Grails 2.4.4。我为我的 Controller 编写了一个集成测试。
class UserControllerIntegrationSpec extends IntegrationSpec {
UserController controller = new UserController()
void "test something"() {
when:
controller.request.method = 'POST'
controller.create()
then:
controller.response.status == HttpStatus.OK.value()
}
}
当我尝试使用 IntelliJ 运行测试时,出现异常:
java.lang.IllegalStateException: Could not find ApplicationContext, configure Grails correctly first
at grails.util.Holders.getApplicationContext(Holders.java:97)
at grails.test.spock.IntegrationSpec.$spock_initializeSharedFields(IntegrationSpec.groovy:41)
当我使用带有 test-app
命令的控制台运行集成测试时,没有问题。附:单元测试工作正常。
最佳答案
您无法像单元测试那样在 IDE 中单独运行集成测试。集成测试环境的运行量几乎与使用 run-app
运行时一样多,其中一个很大的区别是没有 Web 服务器。
因此,测试基础设施期望有一个 Spring 应用程序上下文(至少部分依赖注入(inject)适用于测试类)、GORM、插件等。因此,您可以从 IDE 运行集成测试,但它们必须只需运行 grails test-app
并为您捕获输出,就像在 IDE 中运行应用程序一样。
关于grails - 由于 grails 2.4.4 中的 IllegalStateException 而无法运行集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27356739/