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
  • Labels:
    None
  • Testcase included:
    yes

Description

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
	}
}

Activity

Hide
Richard Scorer added a comment -

Forgot to mention that this code works perfectly in Grails 1.0.4

Show
Richard Scorer added a comment - Forgot to mention that this code works perfectly in Grails 1.0.4
Hide
Richard Scorer added a comment -

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 - 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 -

Affects Grails 1.1 release. Does anyone have a workaround?

Show
Marcus Better added a comment - Affects Grails 1.1 release. Does anyone have a workaround?
Hide
Graeme Rocher added a comment -

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

Show
Graeme Rocher added a comment - Could write an scripts/_Events.groovy with a TestAppStart event that set ConfigurationHolder.config = config
Hide
Marc Palmer added a comment -

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 - 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 -

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 - 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 -

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 - 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 -
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 -
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 -

Added a mockConfig method to help out with testing this:

mockConfig '''
            foo.bar = "good"
        '''
Show
Graeme Rocher added a comment - Added a mockConfig method to help out with testing this:
mockConfig '''
            foo.bar = "good"
        '''

People

Vote (1)
Watch (4)

Dates

  • Created:
    Updated:
    Resolved: