Hes*_*oon 5 grails spring intellij-idea nullpointerexception spock
使用 IDEA 并尝试启动以下代码:
package com.myCompany.routing.spring
import com.dropbox.core.DbxRequestConfig
import grails.util.Holders
import spock.lang.Specification
class DropboxSpringConfigSpec extends Specification {
def grailsApplication=Holders.grailsApplication
def "It instantiates and configures the dropboxRequestConfig component"() {
given:
def ctx = grailsApplication.mainContext
//do stuff...
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
java.lang.NullPointerException:无法获取空对象上的属性“mainContext”
在 com.myCompany.routing.spring.DropboxSpringConfigSpec.It 实例化并配置 dropboxRequestConfig 组件(DropboxSpringConfigSpec.groovy:20)
我最近拉动了我的 VCS,所以代码应该可以工作。
当作为 Grails 测试运行测试时,我收到以下错误:
错误 | 2015-03-04 13:32:00,989 [localhost-startStop-1] 错误 context.GrailsContextLoader - 初始化应用程序时出错:Config.groovy 中缺少配置:connection.uri。
好吧,看来 Config.groovy 中的一些配置被赋予了一些环境变量的值:
elasticSearch {
connection {
uri = env.ES_URL
username = env.ES_USER
password = env.ES_PASSWORD
}
indexPrefix = 'test-'
}
Run Code Online (Sandbox Code Playgroud)
由于我从未创建过相应的环境变量,因此GrailsContextLoader无法找到相应的值,计算失败。
在我的 IDE 中初始化所需的环境变量并以 Grails 测试的方式运行测试解决了问题。