Grails

ConfigurationHolder.config is null for test-app

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Critical Critical
  • Resolution: Fixed
  • Affects Version/s: 1.1-beta2
  • Fix Version/s: 1.1.1
  • Component/s: Testing
  • Testcase included:
    yes
  • Description:
    Hide

    In my test suites, I refer to many configuration variables, and find I cannot run my tests at the moment

    ConfigurationHolder.config seems to be null, causing an NPE when I try to access my config settings

    HelloControllerTests.groovy
    import org.codehaus.groovy.grails.commons.ConfigurationHolder
    
    class HelloControllerTests extends GroovyTestCase {
    
        void testReadingConfig() {
    			println ConfigurationHolder.config.configtest.sayhello
        }
    }
    Unit Test Result

    "Error Cannot get property 'configtest' on null object

    java.lang.NullPointerException: Cannot get property 'configtest' on null object
    at HelloControllerTests.testReadingConfig(HelloControllerTests.groovy:6)
    at _GrailsTest_groovy$_run_closure7_closure22_closure23_closure24.doCall(_GrailsTest_groovy:229)
    at _GrailsTest_groovy$_run_closure7_closure22_closure23_closure24.doCall(_GrailsTest_groovy)
    at _GrailsTest_groovy$_run_closure8_closure30.doCall(_GrailsTest_groovy:297)
    at _GrailsTest_groovy$_run_closure8_closure30.call(_GrailsTest_groovy)
    "

    Putting the same code in a controller has no such problem

    HelloController.groovy
    import org.codehaus.groovy.grails.commons.ConfigurationHolder
    
    class HelloController {
    
        def index = { 
    		render ConfigurationHolder.config.configtest.sayhello
    	}
    }
    Show
    In my test suites, I refer to many configuration variables, and find I cannot run my tests at the moment ConfigurationHolder.config seems to be null, causing an NPE when I try to access my config settings
    HelloControllerTests.groovy
    import org.codehaus.groovy.grails.commons.ConfigurationHolder
    
    class HelloControllerTests extends GroovyTestCase {
    
        void testReadingConfig() {
    			println ConfigurationHolder.config.configtest.sayhello
        }
    }
    Unit Test Result
    "Error Cannot get property 'configtest' on null object java.lang.NullPointerException: Cannot get property 'configtest' on null object at HelloControllerTests.testReadingConfig(HelloControllerTests.groovy:6) at _GrailsTest_groovy$_run_closure7_closure22_closure23_closure24.doCall(_GrailsTest_groovy:229) at _GrailsTest_groovy$_run_closure7_closure22_closure23_closure24.doCall(_GrailsTest_groovy) at _GrailsTest_groovy$_run_closure8_closure30.doCall(_GrailsTest_groovy:297) at _GrailsTest_groovy$_run_closure8_closure30.call(_GrailsTest_groovy) "
    Putting the same code in a controller has no such problem
    HelloController.groovy
    import org.codehaus.groovy.grails.commons.ConfigurationHolder
    
    class HelloController {
    
        def index = { 
    		render ConfigurationHolder.config.configtest.sayhello
    	}
    }
  1. configtest.tar
    (590 kB)
    Richard Scorer
    23/Jan/09 10:59 AM

Activity

Hide
Richard Scorer added a comment - 24/Jan/09 2:29 PM

Forgot to mention that this code works perfectly in Grails 1.0.4

Show
Richard Scorer added a comment - 24/Jan/09 2:29 PM Forgot to mention that this code works perfectly in Grails 1.0.4
Hide
Richard Scorer added a comment - 07/Feb/09 9:04 AM

This still fails in 1.1-beta3

Was trying to modify the "Affects version" but can't seem to do that

Show
Richard Scorer added a comment - 07/Feb/09 9:04 AM This still fails in 1.1-beta3 Was trying to modify the "Affects version" but can't seem to do that
Hide
Marcus Better added a comment - 13/Mar/09 5:24 AM

Affects Grails 1.1 release. Does anyone have a workaround?

Show
Marcus Better added a comment - 13/Mar/09 5:24 AM Affects Grails 1.1 release. Does anyone have a workaround?
Hide
Graeme Rocher added a comment - 13/Mar/09 6:08 AM

Could write an scripts/_Events.groovy with a TestAppStart event that set ConfigurationHolder.config = config

Show
Graeme Rocher added a comment - 13/Mar/09 6:08 AM Could write an scripts/_Events.groovy with a TestAppStart event that set ConfigurationHolder.config = config
Hide
Marc Palmer added a comment - 16/Mar/09 10:03 AM

Isn't this correct behaviour for unit tests - eg including real config = integration testing surely?

Shouldn't all unit tests populate their own settings here? Perhaps config should be an empty ConfigObject, rather than null?

Show
Marc Palmer added a comment - 16/Mar/09 10:03 AM Isn't this correct behaviour for unit tests - eg including real config = integration testing surely? Shouldn't all unit tests populate their own settings here? Perhaps config should be an empty ConfigObject, rather than null?
Hide
Matthew Taylor added a comment - 27/Mar/09 10:09 PM

I am not used to having the config available in my unit tests. I've always mocked out my own configs specifically for unit tests. But this tends to be a problem if you do something like:

ConfigHolder.getConfig = { -> [newConfigKey:'newConfigVal'] }

Because later on in the integration tests, if you are running the whole suite, you won't be able to get your config out. I think there should be a better way to mock config values during unit tests.

Show
Matthew Taylor added a comment - 27/Mar/09 10:09 PM I am not used to having the config available in my unit tests. I've always mocked out my own configs specifically for unit tests. But this tends to be a problem if you do something like:
ConfigHolder.getConfig = { -> [newConfigKey:'newConfigVal'] }
Because later on in the integration tests, if you are running the whole suite, you won't be able to get your config out. I think there should be a better way to mock config values during unit tests.
Hide
Marcus Better added a comment - 28/Mar/09 4:09 PM

I just use

ConfigurationHolder.config = [ foo: 'bar' ]

which seems to work. It's a bit clumsy when the keys have many components.

It would be nice to use ConfigSlurper syntax in unit tests, like this:

mockConfig {
    foo {
      bar.baz = 'value'
    }
  }
Show
Marcus Better added a comment - 28/Mar/09 4:09 PM I just use
ConfigurationHolder.config = [ foo: 'bar' ]
which seems to work. It's a bit clumsy when the keys have many components. It would be nice to use ConfigSlurper syntax in unit tests, like this:
mockConfig {
    foo {
      bar.baz = 'value'
    }
  }
Hide
Matthew Taylor added a comment - 29/Mar/09 6:44 AM
ConfigurationHolder.config = [ foo: 'bar' ]

hasn't worked for me before, that is why I resorted to the metaClass. But I can't tell you under which situations this occurred. I will try this direct assignment method again and post here if I have issues with it. It seems like this is the simplest thing aside from the ConfigSlurper syntax.

Show
Matthew Taylor added a comment - 29/Mar/09 6:44 AM
ConfigurationHolder.config = [ foo: 'bar' ]
hasn't worked for me before, that is why I resorted to the metaClass. But I can't tell you under which situations this occurred. I will try this direct assignment method again and post here if I have issues with it. It seems like this is the simplest thing aside from the ConfigSlurper syntax.
Hide
Graeme Rocher added a comment - 01/May/09 5:32 AM

Added a mockConfig method to help out with testing this:

mockConfig '''
            foo.bar = "good"
        '''
Show
Graeme Rocher added a comment - 01/May/09 5:32 AM Added a mockConfig method to help out with testing this:
mockConfig '''
            foo.bar = "good"
        '''

People

Dates

  • Created:
    23/Jan/09 10:59 AM
    Updated:
    01/May/09 5:32 AM
    Resolved:
    01/May/09 5:32 AM