Grails 2 - Using external configuration in tests

时间:2015-09-01 23:01:39

标签: unit-testing grails grails-2.0 integration-testing

I am not able to access configurations properites from grails-app/conf/Config.groovy in my Unit Tests and Integration Tests.

The properties does not exist in grailsApplication.config object

Using Grails 2.5.1 and IntelliJ IDEA 14

What's the best practice to configure my Unit Tests and Integration Tests with Grails 2?

2 个答案:

答案 0 :(得分:1)

对于单元测试: 这是正常的行为,因为这是一个外部依赖,你不应该依赖它,而是将其存在。 UnitTestMixin在单元测试中提供config变量,您可以使用它来存储值,即config.myValue = xx

对于集成测试:grailsApplication.config(只是在测试中注入grailsApplication)应该像往常一样可以访问。您可以更改配置值,但不要忘记在测试后清理(即恢复到原始值)。

答案 1 :(得分:0)

Solution 1:

IntelliJ > File menu > Project Structure > Modules

Add grails-app/conf folder to Test Source Folders

Solution 2:

Create a separated config file for running tests.

Put a Config.groovy file in test/unit folder and another Config.groovy file in test/integration folder.

The properties will be loaded into grailsApplication.config object