我正在尝试测试将生成pdf文件的服务类,但会发生以下错误:
[getDocument] EXCEPTION:变量:[输入,输入,输入],消息: 无法在null对象上获取属性'config'
我的服务类是:
Outer
这是我的测试: *注意输入是一个arraylist
class TemplateService {
static transactional = false
def grailsApplication
def getDocument(inputs, idTemp) {
def result
if(inputs) {
long dateBeginTransaction = System.currentTimeMillis()
try {
def http = new HTTPBuilder(grailsApplication.config.tempdoc.url?.replace("COI", idTemp))
http.auth.basic grailsApplication.config.rest.login, grailsApplication.config.rest.password
http.request(POST,JSON) { req ->
headers.'Accept' = 'application/json'
headers.'Content-type' = 'application/json'
body = [
inputs: inputs
]
response.success = { resp, json ->
log.info "[getDocument] time: " + (System.currentTimeMillis() - dateBeginTransaction) / 1000 + " ms"
result = json?.pdf
}
response.failure = { resp, reader ->
log.error "[getDocument] inputs: " + inputs + ", response: " + resp + ", message: " + reader?.message
}
}
} catch (Exception e) {
log.error "[getDocument] EXCEPTION: inputs: " + inputs + ", message: " + e.message
}
} else {
log.error "[getDocument] params sense valors"
}
result
}
}
答案 0 :(得分:0)
至少,您必须在测试中模拟配置。在Grails 3.3.5中:
class PlaServiceSpec extends Specification implements ServiceUnitTest<Plaservice>, DataTest {
Closure doWithConfig() {{ config ->
config.plantidoc.url = 'my url'
}}
void "generate document"() {
given:
def variables = ["input:input", "input:input"]
def idPlantitlla = "12AD"
when:
def result = service.getDocument(variables, idPlantitlla)
then:
// test the result
}
}
答案 1 :(得分:0)
您可以将测试作为集成测试(将其移至测试/集成测试文件夹),这样grailsApplication将被注入您的服务