【问题标题】:Testing Grails App Generating pdf file Cannot get property 'config' on null object测试 Grails 应用程序生成 pdf 文件无法在空对象上获取属性“配置”
【发布时间】:2018-06-13 11:59:31
【问题描述】:

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

[getDocument] 例外:变量:[输入,输入,输入],消息: 无法在空对象上获取属性“配置”

我的服务类是:

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
    }
}

这是我的测试: *注意输入是一个数组列表

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"
}

【问题讨论】:

    标签: java grails integration-testing


    【解决方案1】:

    至少,您必须在测试中模拟配置。在 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
        }
    }
    

    【讨论】:

    • a 只是模拟它,但同样的错误: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, idPlantitlla) 其中: variables = ["input:input", "input:input"] idPlantitlla = "12AD " }
    • 您使用的是哪个版本的 grails?
    • grailsVersion=3.3.5.
    • 我可以联系你吗?或者你可以联系我吗:broganzquinto@gmail.com
    • 我尝试了您的代码,但错误仍然存​​在:测试工作人员] cat.dipta.mfi.PlantidocService : [getDocument] 例外:变量:[numeroExpedient:numeroExpedient, actuacio:actuacio, fase:fase], 消息: 无法在空对象上获取属性“配置”
    【解决方案2】:

    您可以将您的测试设为集成测试(将其移至 test/integration-test 文件夹),这样 grailsApplication 将被注入您的服务中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 2022-01-13
      相关资源
      最近更新 更多