The problem with getting grails Link to the application from the integration test

I am using Grails 1.2.1 and trying to write an integration test for one of my service classes. The service method I want to test is ...

class UtilityService { boolean transactional = false def grailsApplication def isAuthorizedHost(String hostIpAddr) { // Simple validation if (hostIpAddr == null || hostIpAddr.length() == 0) return false; // def allowedDomains = grailsApplication.config.proxy.allowedDomains ... 

but when writing my integration test, I cannot get a non-zero reference to the grailsApplication object ...

 class UtilityServiceTests extends GrailsUnitTestCase { def grailsApplication void testIsAuthorizedHost() { def utilityService = new UtilityService() utilityService.grailsApplication = grailsApplication def ret = utilityService.isAuthorizedHost("127.0.0.1") assertTrue( ret ) } 

Here is the error. How to get a link? - Dave

Unable to get 'config' property for null object

java.lang.NullPointerException: cannot get property 'config' on null object at com.nna.tool.proxy.Utility.UtilityService.isAuthorizedHost (UtilityService.groovy: 26) at com.nna.tool.proxy.Utility.UtilityService $ isAuthorizedHost.call (Unknown source) at com.nna.tool.proxy.Utility.UtilityServiceTests.testIsAuthorizedHost (UtilityServiceTests.groovy: 20)

+4
source share
4 answers

See the answer here. This may also work in your situation. You can simply put this code in the setup () method of the tests ...

Grails functional testing - grailsApplication.config is null within controllers and services

+2
source

I think the grailsApplication property is only available in the controller and views for the service

Try ApplicationHolder.application.config.proxy.allowedDomains .

+1
source

You need to run integration tests because Grails tests are not jUnit.

+1
source

Building grailsApp with DefaultGrailsApplication will work.

 import org.codehaus.groovy.grails.commons.DefaultGrailsApplication class UtilityServiceTests extends GrailsUnitTestCase { def grailsApplication = new DefaultGrailsApplication() void testIsAuthorizedHost() { def utilityService = new UtilityService() utilityService.grailsApplication = grailsApplication def ret = utilityService.isAuthorizedHost("127.0.0.1") assertTrue( ret ) } } 

References

grails-core / grails-core / src / main / groovy / org / codehaus / groovy / grails / commons / DefaultGrailsApplication.java

+1
source

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


All Articles