[英]grailsApplication not available in Domain class when running all Integration tests
I'm running into a problem when running integration tests in Grails.在 Grails 中运行集成测试时遇到问题。 One of the domain classes in the project has a method that accesses the
grailsApplication.config
property.项目中的域类之一具有访问
grailsApplication.config
属性的方法。 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
The test runs fine and the domain class can access grailsApplication
.测试运行良好,域类可以访问
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
When trying to access grailsApplication.config
.尝试访问
grailsApplication.config
。 It seems that when the tests are all run together grailsApplication
is not being injected into the domain class.似乎当所有测试一起运行时
grailsApplication
没有被注入到域类中。 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.从我读到的内容来看,这似乎是一个测试污染问题,但由于每个集成测试中都没有进行设置,因此很难找到问题所在。
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:我在 grails 3 中遇到了这个问题。如果您正在测试域,则必须在测试开始时进行设置(使用 spock 进行测试):
def setup() {
Holders.grailsApplication = new DefaultGrailsApplication()
Holders.grailsApplication.config.property.code = '1234'
}
I'm not sure what the issue is, but this must be a test pollution issue where yours any one of the test case is doing something with grailsApplication
using meta class.我不确定问题是什么,但这一定是一个测试污染问题,您的任何一个测试用例都使用元类对
grailsApplication
执行某些grailsApplication
。 Because this seems to work fine for me.因为这对我来说似乎很好用。
As a workaround you can avoid using injected grailsApplication
and use the Holders
class instead like the following:作为一种解决方法,您可以避免使用注入的
grailsApplication
并使用Holders
类,如下所示:
package com
import grails.util.Holders
class User {
// def grailsApplication
String email
String name
String getTitle() {
return Holders.getConfig()["app.title.prefix"] + name
// Or
//return Holders.grailsApplication.config["app.title.prefix"] + name
}
}
This way, you are not dependent upon the dependency injection.这样,您就不会依赖于依赖注入。
我遇到了同样的问题,发现我的集成测试实现了导致问题的 ServiceUnitTest<>。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.