0

我有一个实现 InitializingBean 和 DisposableBean 的服务

class MyService implements InitializingBean, DisposableBean {

    static transactional = false

    def grailsApplication

    @Override
    void afterPropertiesSet() {
        System.setProperty("JMS_TIMEOUT", grailsApplication.config.JMS_TIMEOUT);
        // code performing a JDNI lookup
    }
}
enter code here

系统属性用于初始化服务中的一些其他组件。我已经在 Config.groovy 中添加了配置。

grails.config.locations = [ "file:${basedir}/grails-app/conf/myconfig.properties" ]

这在运行应用程序时工作正常。但是,我正在测试/集成中编写一个注入服务的集成测试。

class MyServiceIntegrationTests  extends GrailsUnitTestCase {

   def myService

   void testMyService() {

   }
}

运行测试时,我得到一个 StackTrace,其根本原因如下:

Caused by: javax.naming.NameNotFoundException: Name [ConnectionFactory] not bound; 0 bindings: []
at javax.naming.InitialContext.lookup(InitialContext.java:354)
at com.ubs.ecredit.common.jmsclient.DefaultConnector.<init>(DefaultConnector.java:36)

似乎无法加载配置或在集成测试中有所不同。知道如何更改配置或代码,以便在实例化服务之前也为我的集成测试设置这些属性吗?

UPDATE: It turned out the cause was not the configurations but a JDNI lookup and a bug in Grails. See: http://jira.grails.org/browse/GRAILS-5726

4

2 回答 2

1

${basedir} gets different paths in different environments. As an alternative, you can use PropertiesLoaderUtils.loadProperties to load your customized configurations:

import org.springframework.core.io.support.PropertiesLoaderUtils
import org.springframework.core.io.ClassPathResource
....
void afterPropertiesSet() {
    def configProperties = PropertiesLoaderUtils.loadProperties(
                   new ClassPathResource("myconfig.properties"))
    System.setProperty("JMS_TIMEOUT", configProperties.getProperty("JMS_TIMEOUT"))
    ....
}
于 2013-01-10T03:23:37.027 回答
0

It turned out the cause was a JNDI lookup used by a library method, I have not shown in afterPropertiesSet() in my Service, which can be seen in the StackTrace.

After doing some research I found that this was a bug in Grails: http://jira.grails.org/browse/GRAILS-5726

Adding the mentioned workaround, resolved the issue for now.

于 2013-01-16T14:35:20.873 回答