运行所有集成测试时,grailsApplication在Domain类中不可用 [英] grailsApplication not available in Domain class when running all Integration tests

查看:84
本文介绍了运行所有集成测试时,grailsApplication在Domain类中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Grails中运行集成测试时遇到问题.项目中的一个域类具有访问grailsApplication.config属性的方法.我们对在域类中调用此方法的一项服务进行了集成测试.使用命令单独运行测试时

I'm running into a problem when running integration tests in Grails. One of the domain classes in the project has a method that accesses the grailsApplication.config property. We have an integration test for one of our services that call this method in the domain class. When the test is run on its own using the command

grails test-app integration: com.project.MyTestSpec

测试运行正常,并且域类可以访问grailsApplication.但是,当使用以下命令运行整个测试套件时:

The test runs fine and the domain class can access grailsApplication. However when the whole test suite is run using:

grails test-app integration

然后测试失败并显示错误

then the test fails with the error

java.lang.NullPointerException: Cannot get property 'config' on null object

尝试访问grailsApplication.config时.似乎所有测试都一起运行时,grailsApplication并没有注入到域类中.有人遇到过这个问题吗?

When trying to access grailsApplication.config. It seems that when the tests are all run together grailsApplication is not being injected into the domain class. Has anyone come across this issue before?

从我所阅读的内容来看,这似乎是一个测试污染问题,但是由于每个集成测试中都没有进行设置,因此很难找到问题所在.

From what I have read it seems to be a test pollution issue but as there is no setup happening in each integration test the problem is hard to track down.

推荐答案

我遇到了grails 3这个问题.如果要测试域,则必须在测试开始时进行设置(使用spock进行测试):

I came across this issue with grails 3. If you are testing a Domain, you have to setup (testing with spock) at the begining of the test:

def setup() {
        Holders.grailsApplication = new DefaultGrailsApplication()
        Holders.grailsApplication.config.property.code = '1234'
    }

这篇关于运行所有集成测试时,grailsApplication在Domain类中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆