简体   繁体   English

Grails:用于需要grailsApplication.config句柄的测试的实用程序类

[英]Grails: Utility class for tests that needs handle to grailsApplication.config

I have a bunch of integration tests. 我有很多集成测试。 I want a class that has some utility methods that the tests use. 我想要一个包含一些测试使用的实用程序方法的类。 I started off making the utility methods all static and it was easy to get a handle to them from the tests. 我开始将实用程序方法全部静态化,并且很容易从测试中找到它们。

However, I needed the utility class to be able to get a handle on the grailsApplication.confg so I could pull in some config. 但是,我需要实用程序类才能获取grailsApplication.confg的句柄,以便可以进行一些配置。 To do this, because the utility class is not a service, I made the class implement ApplicationContextAware . 为此,因为实用程序类不是服务,所以我使该类实现了ApplicationContextAware This meant I had to make the static methods instance based. 这意味着我必须基于静态方法实例。 Fair enough. 很公平。

To make it ApplicationContextAware, I implement interface and I do: 为了使其成为ApplicationContextAware,我实现了接口,然后执行以下操作:

class TestUtils {
    def grailsApplication
    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
        throws BeansException {
           // TODO Auto-generated method stub
          this.grailsApplication = grailsApplication
    }
    ...

But this never gets called meaning my local grailApplication ref is never set. 但这从未被调用,这意味着从未设置我的本地grailApplication引用。

Is it better if I just made this TestUtility field a service or what are my doing wrong? 如果我只是将此TestUtility字段提供服务会更好,还是我做错了什么?

您可以使用静态方法保留实用程序类,并通过Holders类访问grailsApplication

def grailsApplication = Holders.grailsApplication

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Grails功能测试 - grailsApplication.config在控制器和服务中为空 - Grails Functional Testing - grailsApplication.config is null within controllers and services 如何从groovy类访问grailsApplication.config? - How to access grailsApplication.config from a groovy class? 将字符串传递给服务层中的grailsApplication.config - pass a string to grailsApplication.config in service layer 为什么在Config.groovy中定义的变量没有出现在.gsp页面的grailsApplication.config对象中? - Why doesn't my variable defined in Config.groovy appear in the grailsApplication.config object in a .gsp page? Grails 单元测试:通过 grailsApplication 访问定义的 bean - Grails unit tests: Accessing defined beans via grailsApplication 运行所有集成测试时,grailsApplication 在域类中不可用 - grailsApplication not available in Domain class when running all Integration tests 代码在grailsApplication.config.grails.binRange处失败。 谁能解决这个问题? - code fail at grailsApplication.config.grails.binRange. any one can solve this? grails单元测试中的grailsApplication访问 - grailsApplication access in Grails unit Test 使用Grails的Grails单元测试 - Grails unit test using grailsApplication Grails 3.3.11 集成测试(GrailsApplication) - Grails 3.3.11 Integration Test (GrailsApplication)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM