Java 测试生成pdf文件的Grails应用程序无法获取属性';配置';关于空对象

Java 测试生成pdf文件的Grails应用程序无法获取属性';配置';关于空对象,java,grails,integration-testing,Java,Grails,Integration Testing,我正在尝试测试将生成pdf文件的服务类,但出现以下错误: [getDocument]异常:变量:[输入,输入,输入],消息: 无法获取null对象上的属性“config” 我的服务级别是: class TemplateService { static transactional = false def grailsApplication def getDocument(inputs, idTemp) { def result if(in

我正在尝试测试将生成pdf文件的服务类,但出现以下错误:

[getDocument]异常:变量:[输入,输入,输入],消息: 无法获取null对象上的属性“config”

我的服务级别是:

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
    }
}
这是我的测试: *注意:输入是一个arraylist

void "generate document"() {
    given: "generate document"
        def TemplateService = new TemplateService()

    when:
        def result = TemplateService.getDocument(inputs, idTemp)

    then:
        result != null
        result.size() > 0   

    where:
       inputs = [ "input", "input", "input"]
        idTemp =  "12AD"
}

至少,您必须在测试中模拟配置。在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
    }
}
类PlaServiceSpec扩展规范实现ServiceUnitTest、DataTest{ 闭包doWithConfig(){{config-> config.plantidoc.url='我的url' }} 作废“生成文档”(){ 鉴于: def变量=[“输入:输入”,“输入:输入”] def idplantitla=“12AD” 什么时候: def result=service.getDocument(变量,idPlantitlla) 然后: //测试结果 } }
您可以将您的测试设置为集成测试(将其移动到test/Integration test文件夹),这样GrailApplication将被注入到您的服务中

一个简单的模拟,但同样的错误:def setup(){grailsApplication.config.plantidoc.url='my url'}def cleanup(){}void“generate document”(){给定:“生成文档”def plaservice=new plaservice()plantidocService.grailsApplication=grailsApplication时:def result=plaservice.getDocument(variables,idplantitla),其中:variables=[“input:input”,“input:input”]idplantitla=“12AD”}您使用的是哪一版本的grails?grailsVersion=3.3.5。我可以与您联系吗,或者您可以告诉我:broganzquinto@gmail.comI尝试了您的代码,但错误仍然存在:Test worker]cat.dipta.mfi.PlantidocService:[getDocument]异常:变量:[numeroExpedient:numeroExpedient,actuacio:actuacio,fase:fase],消息:无法获取null对象的属性“config”