简体   繁体   中英

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. To do this, because the utility class is not a service, I made the class implement ApplicationContextAware . This meant I had to make the static methods instance based. Fair enough.

To make it ApplicationContextAware, I implement interface and I do:

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.

Is it better if I just made this TestUtility field a service or what are my doing wrong?

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

def grailsApplication = Holders.grailsApplication

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM