Layout configuration in Grails tests

Prior to Grails 2.0.X, you could mock the configuration using the mockConfig method, provided by the base class that is being tested. However, Grails 2.0.X recommends that test classes use @TestFor mixin instead of extending the base class.

This mixin doesn't seem to give anything equivalent to the mockConfig methods, so I can't figure out how to mock the values ​​in Config.groovy .

+6
source share
4 answers

You have access to grailsApplication.config so you can change these values ​​as much as you need, so you can do

 grailsApplication.config.some.config.setting = 'foo' 
+6
source

I do it like this (in the case when I test the service):

 service.grailsApplication.config.mysetting = 'my value' def result = service.myMethod() // check results 

No other ridicule is required.

+4
source

It seems that Grails 2.x tests already have a config object. Therefore enough to say

 config.some.config.setting = 'foo' 

which will then be available from the @TestFor test object.

If you need to create an object manually, then the test also has a grailsApplication object in the object area to which the config object belongs. So this should work -

 config.some.config.setting = 'foo' def myService = new MyService(grailsApplication:grailsApplication) 
+1
source

It seems that mockConfig () no longer exists in Grails 2.x. how about this:

  def mockConfig = new ConfigObject() mockConfig.some.config.setting = "foo" def myService = new MyService() myService.grailsApplication = [config: mockConfig] 
0
source

Source: https://habr.com/ru/post/916603/