简体   繁体   English

运行所有集成测试时,grailsApplication 在域类中不可用

[英]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.

相关问题 Grails:用于需要grailsApplication.config句柄的测试的实用程序类 - Grails: Utility class for tests that needs handle to grailsApplication.config Grails 3.3.11 集成测试(GrailsApplication) - Grails 3.3.11 Integration Test (GrailsApplication) 运行集成测试时不存在Grails动态方法 - Grails dynamic methods not present when running integration tests 在groovy类中访问grailsApplication或Service - Access grailsApplication or Service in groovy class 使用Gradle运行Grails集成测试时定位特定的程序包 - Target specific package when running Grails integration tests using Gradle 无法从集成测试中获取grailsApplication参考 - Trouble getting grailsApplication reference from integration test grails应用程序自动装配到spock测试始终为null - grailsApplication autowiring to spock tests are always null 如何在功能测试中访问grailsApplication和ApplicationContext - How to access grailsApplication and ApplicationContext in Functional Tests 与其他启用了事务的集成测试一起运行时,集成测试失败 - An integration test fails when running with other integration tests which have transactional enabled 在Intellij中运行grails 2.1.3测试Idea:Spock测试中的奇怪错误:无法添加Domain类[class xyZ]。 它不是域名 - running grails 2.1.3 tests in Intellij Idea: Bizarre error in Spock test: Cannot add Domain class [class x.y.Z]. It is not a Domain
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM