Sean LeBlanc
Sean LeBlanc

Reputation: 195

Unit testing grails ConfigSlurper behavior

I'd like to write tests that would test behavior of externalized configs and assert that what gets set is what I expect. This is for the specific case where something like this is done:

Config.groovy:

a.reused.value = 'orig' my.variable = '${a.reused.value}'

Externalized groovy file:

a.reused.value = 'new_value'

I expect that both a.reused.value and my.variable would be 'new_value'.

Now, I think I could have my unit test read in strings representing these config files (I do similar things for other unit tests to populate Holders.grailsApplication.config, for example), utilizing perhaps merge?

But what I cannot figure out is how to get the value that Grails actually gets during application run time. Instead, I get "${a.reused.value}" in my unit tests.

Is there a way to mimic this behavior of what Grails does of actually resolving this value? I did some digging around in Grails 2.4.4 source (which is what we are using) and didn't have any luck in figuring this part out. I also did try Eval.me(), but that doesn't seem to be quite right either.

Upvotes: 0

Views: 59

Answers (1)

Sandeep Poonia
Sandeep Poonia

Reputation: 2188

While setting my.variable, you are not using a GString object, causing the expression to be treated as a value itself. Use double quotes to resolve expression automatically.

a.reused.value = 'orig' my.variable = "${a.reused.value}"

Update 1:

What you want to do is directly not possible. You are assigning the value to a variable from an expression. During evaluation of the config object for the first time, my.variable has been assigned a value, and now it doesn't contain an expression any more. So you have two options: 1) either reassign the second variable in external config also or 2) use a closure to assign the value to second variable.

my.variable = { -> "$a.reused.value" }

and while accessing do: grailsApplication.config.my.variable.call() But again, in your code, you would have to be sure that this variable contains a closure not a value itself.

Upvotes: 0

Related Questions