如何在Groovy Spock测试中模拟httprequest

时间:2019-06-28 12:01:30

标签: groovy xmlhttprequest spock

我需要对httprequest进行模拟,以响应测试该类时创建的httprequest。

我的课程对服务使用httprequest(基于SOAP XML)进行创建,读取和更新。该测试最终将在Jenkins上运行,并使用maven在本地运行。

要测试的代码:

def callService(request, action, tableName) {
    def response = httpRequest consoleLogResponseBody: true, ccustomHeaders: [[maskValue: false, name: 'SOAPAction', value: "urn:${action}"], [maskValue: false, name: 'Content-Type', value: 'text/xml;charset=UTF-8']],
    authentication: 'xyz_ab', httpMode: 'POST',  requestBody: request, url: "https://serviceurl.com/drft_${tableName}_${getActionUrn(action)}.do?hierarchical=true&SOAP", validResponseCodes: '200,201'

    def parsedResp = new XmlSlurper().parseText(response.getContent()).declareNamespace(soap:'http://schemas.xmlsoap.org/soap/envelope/')

    return parsedResp.'soap:Body'
}

测试代码:

        when:
            result == TestClassObj.callService(request, action, tableName)

        then:
            1* getMockPipeline('httpRequest')

当前的测试代码无法模拟httpRequest。关于Maven测试的错误如下:

groovy.lang.MissingMethodException: No signature of method: package.class.scriptName.httpRequest() is applicable for argument types: (java.util.LinkedHashMap) values: [[consoleLogResponseBody:true, ccustomHeaders:[[maskValue:false, ...], ...], ...]]
        at package.class.scriptNameTest.callService test(scriptNameTest.groovy:60)

0 个答案:

没有答案