Loading Config.groovy properties from unit test cases

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Loading Config.groovy properties from unit test cases

dave.alvarado
Hi,

In some of my unit test cases, I need to refer to properties I set for my application, which I currently do in Config.groovy.  For example ...

proxy.cacheRefreshInterval=15
proxy.globalCacheRefreshInterval=15

However, in my unit test cases, I am currently mocking the configuration objects, and re-hard-coding the properties ...

                // Set mock config objects
                def mockedConfig = new ConfigObject()
                mockedConfig.proxy.cacheRefreshInterval=15
                mockedConfig.proxy.globalCacheRefreshInterval=15
                ConfigurationHolder.config = mockedConfig

I would prefer not to hard-code values twice, but I don't know how to read in the properties I have set in the Config.groovy file.  Any ideas on how to prevent duplication of code? - Dave
Reply | Threaded
Open this post in threaded view
|

Re: Loading Config.groovy properties from unit test cases

rlovtangen
Not an answer to your question, but inside a GrailsUnitTestCase you can alternatively use mockConfig:

         mockConfig '''
            proxy.cacheRefreshInterval=15
                proxy.globalCacheRefreshInterval=15
        '''

I'm interested in the answer to your question myself.

Ronny


On Apr 5, 2011, at 4:35 PM, dave.alvarado wrote:

> Hi,
>
> In some of my unit test cases, I need to refer to properties I set for my
> application, which I currently do in Config.groovy.  For example ...
>
> proxy.cacheRefreshInterval=15
> proxy.globalCacheRefreshInterval=15
>
> However, in my unit test cases, I am currently mocking the configuration
> objects, and re-hard-coding the properties ...
>
> // Set mock config objects
> def mockedConfig = new ConfigObject()
> mockedConfig.proxy.cacheRefreshInterval=15
> mockedConfig.proxy.globalCacheRefreshInterval=15
> ConfigurationHolder.config = mockedConfig
>
> I would prefer not to hard-code values twice, but I don't know how to read
> in the properties I have set in the Config.groovy file.  Any ideas on how to
> prevent duplication of code? - Dave
>
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Loading-Config-groovy-properties-from-unit-test-cases-tp3428212p3428212.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Loading Config.groovy properties from unit test cases

rlovtangen
In reply to this post by dave.alvarado
Maybe something like this:

        void testFoo() {
                loadConfig()
                assert something
        }

        private void loadConfig() {
                GroovyClassLoader classLoader = new GroovyClassLoader(this.class.classLoader)
                ConfigSlurper slurper = new ConfigSlurper(Environment.current.name)
                ConfigurationHolder.config = slurper.parse(classLoader.loadClass("Config"))
        }

Did work on command line, but did not work by default in IntelliJ because grails-app/conf is not a source or testsource folder. Added grails-app/conf as a testsource folder in IntelliJ, and then it worked.
Maybe Config.groovy could be read from disk rather than from classpath..

Ronny

On Apr 5, 2011, at 4:35 PM, dave.alvarado wrote:

> Hi,
>
> In some of my unit test cases, I need to refer to properties I set for my
> application, which I currently do in Config.groovy.  For example ...
>
> proxy.cacheRefreshInterval=15
> proxy.globalCacheRefreshInterval=15
>
> However, in my unit test cases, I am currently mocking the configuration
> objects, and re-hard-coding the properties ...
>
> // Set mock config objects
> def mockedConfig = new ConfigObject()
> mockedConfig.proxy.cacheRefreshInterval=15
> mockedConfig.proxy.globalCacheRefreshInterval=15
> ConfigurationHolder.config = mockedConfig
>
> I would prefer not to hard-code values twice, but I don't know how to read
> in the properties I have set in the Config.groovy file.  Any ideas on how to
> prevent duplication of code? - Dave
>
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Loading-Config-groovy-properties-from-unit-test-cases-tp3428212p3428212.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Loading Config.groovy properties from unit test cases

rlovtangen
Wait a minute..
Environment.current.name will return "development" (default value I guess). when run as JUnit test in IDEs.
IntelliJ has the option to run as Grails test, which is effectively the same as running from the command line, but that's super slow (25 seconds compared to less than 1 second on my machine), so you normally wouldn't do that.
But since this is inside a test anyway, we could probably use "test" or Environment.TEST.name directly, rather than Environment.current.name. Or maybe let loadConfig() take an optional String param if we want to load config for other environments.

Ronny

On Apr 5, 2011, at 5:16 PM, Ronny Løvtangen wrote:

> Maybe something like this:
>
> void testFoo() {
> loadConfig()
> assert something
> }
>
> private void loadConfig() {
> GroovyClassLoader classLoader = new GroovyClassLoader(this.class.classLoader)
> ConfigSlurper slurper = new ConfigSlurper(Environment.current.name)
> ConfigurationHolder.config = slurper.parse(classLoader.loadClass("Config"))
> }
>
> Did work on command line, but did not work by default in IntelliJ because grails-app/conf is not a source or testsource folder. Added grails-app/conf as a testsource folder in IntelliJ, and then it worked.
> Maybe Config.groovy could be read from disk rather than from classpath..
>
> Ronny
>
> On Apr 5, 2011, at 4:35 PM, dave.alvarado wrote:
>
>> Hi,
>>
>> In some of my unit test cases, I need to refer to properties I set for my
>> application, which I currently do in Config.groovy.  For example ...
>>
>> proxy.cacheRefreshInterval=15
>> proxy.globalCacheRefreshInterval=15
>>
>> However, in my unit test cases, I am currently mocking the configuration
>> objects, and re-hard-coding the properties ...
>>
>> // Set mock config objects
>> def mockedConfig = new ConfigObject()
>> mockedConfig.proxy.cacheRefreshInterval=15
>> mockedConfig.proxy.globalCacheRefreshInterval=15
>> ConfigurationHolder.config = mockedConfig
>>
>> I would prefer not to hard-code values twice, but I don't know how to read
>> in the properties I have set in the Config.groovy file.  Any ideas on how to
>> prevent duplication of code? - Dave
>>
>>
>> --
>> View this message in context: http://grails.1312388.n4.nabble.com/Loading-Config-groovy-properties-from-unit-test-cases-tp3428212p3428212.html
>> Sent from the Grails - user mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>   http://xircles.codehaus.org/manage_email
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Loading Config.groovy properties from unit test cases

dave.alvarado
In reply to this post by rlovtangen
Outstanding!  Works perfectly.  Thanks for your help, - Dave
Reply | Threaded
Open this post in threaded view
|

Re: Loading Config.groovy properties from unit test cases

cats

But if you have lots of unit tests, watch out for memory leaks using the slurper. See http://grails.1312388.n4.nabble.com/STS-grails-junit-memory-leak-td3262084.html

Cheers!
Rob
Reply | Threaded
Open this post in threaded view
|

Re: Loading Config.groovy properties from unit test cases

David001
In reply to this post by dave.alvarado
I am migrating an application from Grails1.x to 2.3.9.  I am tasked with getting the unit tests to run; then to correct any tests that fail. How can I get a unit test to see the Config.groovy?  Am I loading it correctly below?

I have a unit test for an object that uses a shared constraint.  The constraint is in Config.groovy.  When I run the unit test I get this error:
|Running 1 unit test... 1 of 1
--Output from constraints--
Failure:  |
constraints(com.kpi.dp.xxxx.renderTypes.EmailCommandSpec)
 |
org.codehaus.groovy.grails.exceptions.GrailsConfigurationException: Property [com.kpi.dp.xxxxx.DpCatalogFormInputType.name] references shared constraint [notBlank:null], which doesn't exist!

In the unit test I am trying this code:
--
class EmailCommandSpec extends UnitSpec {

    def "constraints"() {
        //def notBlank = grailsApplication.config.grails.gorm.default.constraints.notBlank
        loadConfig()

        setup:
        mockForConstraintsTests(EmailCommand)
        mockDomain(DpCatalogFormInput)
        mockDomain(DpCatalogFormInputType)
         BaseFormCommand.metaClass.static.isVisible = {arg -> return true}
...
    private void loadConfig() {
        GroovyClassLoader classLoader = new GroovyClassLoader(this.class.classLoader)
        ConfigSlurper slurper = new ConfigSlurper(Environment.TEST.name)
        ConfigurationHolder.config = slurper.parse(classLoader.loadClass("Config"))
    }

--