找不到 com.eviware.soapui.support.XmlHolder(java.io.StringWriter) 的匹配构造函数

Could not find matching constructor for com.eviware.soapui.support.XmlHolder(java.io.StringWriter)

我正在尝试将数据库值与 soapui 上的 API 值进行比较,测试用例相同但步骤不同。

第一步从数据库中存储一个列表,第二步将值与 API

进行比较

数据库响应:

<Results>
   <ResultSet fetchSize="128">
      <Row rowNumber="1">
         <PortalId>87776</PortalId>
         <ItemKey>Asset</ItemKey>
         <ItemValue>Customer Equipment</ItemValue>
         <ItemPluralValue>Customer Equipment</ItemPluralValue>
      </Row>
      <Row rowNumber="2">
         <PortalId>87776</PortalId>
         <ItemKey>AssignedBy</ItemKey>
         <ItemValue>Assigned By</ItemValue>
         <ItemPluralValue>Assigned By</ItemPluralValue>
      </Row>
      <Row rowNumber="3">
         <PortalId>87776</PortalId>
         <ItemKey>AssignedTo</ItemKey>
         <ItemValue>Assign/Appointment</ItemValue>
         <ItemPluralValue>Assign/Appointment</ItemPluralValue>
      </Row>
   </ResultSet>
</Results>

保存数据库响应的脚本:

import com.eviware.soapui.support.XmlHolder
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase

//Create the xml holder object for the jdbc response
def holder = new XmlHolder(messageExchange.responseContent)


def nodes = holder.getDomNodes( "//Row" )

// create list of XML strings
def list = []

for( node in nodes )
{
   java.io.StringWriter writer = new java.io.StringWriter()
   com.eviware.soapui.support.xml.XmlUtils.serialize( node, writer )
   list.add( writer )
}

// store in context for later access
//this is really hacky
WsdlTestCase.metaClass.myList = list

这按预期工作

然后脚本在其他步骤中比较值

JSON 回复:

{
   "totalRows": 32,
   "results":    [
            {
         "key": "Asset",
         "value": "Customer Equipment",
         "pluralValue": "Customer Equipment",
         "portalId": 87776
      },
            {
         "key": "AssignedBy",
         "value": "Assigned By",
         "pluralValue": "Assigned By",
         "portalId": 87776
      },
            {
         "key": "AssignedTo",
         "value": "Assign/Appointment",
         "pluralValue": "Assign/Appointment",
         "portalId": 87776
      }]
}

第二个脚本;

import com.eviware.soapui.support.XmlHolder
import net.sf.json.groovy.JsonSlurper

//get dictList from previous step
def dictList = context.testCase.myList

//get json from response
def jsonResponse = new JsonSlurper().parseText(messageExchange.responseContent)

//put json response to an array
def list = jsonResponse.results 

//enumarate the array
list.eachWithIndex { item, index ->

        def dict = dictList[index]
        def obj = item

    //put the dict xml to a var
    def holder = new XmlHolder(dict)

    //compare the database values to API Values
    assert obj.key == holder.getNodeValue('//*:ItemKey') 
    assert obj.value == holder.getNodeValue('//*:ItemValue') 
    assert obj.pluralValue == holder.getNodeValue('//*:ItemPluralValue') 
    assert obj.portalId.toString() == holder.getNodeValue('//*:PortalId')
}

现在两个脚本都有 import com.eviware.soapui.support.XmlHolder

但是第二个脚本失败并出现以下错误:

Could not find matching constructor for com.eviware.soapui.support.XmlHolder(java.io.StringWriter)

为什么?

在您创建 new java.io.StringWriter() 列表的第一个脚本中 并将其存储到 myList

在第二个脚本中,您尝试为 myList 的每个项目创建 com.eviware.soapui.support.XmlHolder,但在此 class 中没有接受 StringWriter 作为参数的构造函数。

只需在您的第一个脚本中更改此行:

list.add( writer )

到这个

list.add( writer.toString() )

这是 Json 测试步骤的脚本断言。

更多详情请关注在线评论。

//Script Assertion for the Json Step
assert context.response, 'Response is empty or null'

//Replace the JdbcStepName in the below
def parsedXml = new XmlSlurper().parseText(context.testCase.testSteps['JdbcStepName'].responseContent)

//Json response - which is current step
def parsedJson = new groovy.json.JsonSlurper().parseText(context.response)

//Sort the list of maps using below criteria
def sortByKey = {a, b -> a.key <=> b.key }

//Create the list from jdbc xml response and sort it
def buildXmlDataList = {
    parsedXml.'**'.findAll{ it.name() == 'Row'}.collect{ [key: it.ItemKey.text(), value: it.ItemValue.text(), pluralValue: it.ItemPluralValue.text(), portalId: it.PortalId.text() as Integer]}.sort(sortByKey)
} 

//Create the list from json response and sort it
def buildJsonDataList = {
    parsedJson.results.sort(sortByKey)
}

//Pring it; if using in soapui then use log.info instead of println
println buildXmlDataList()
println buildJsonDataList()

//Assert both the data
assert buildXmlDataList() == buildJsonDataList()

请注意,上面的脚本确实直接读取了 Jdbc Xml 响应,这与您使用并非真正需要的元类将其存储在 WsdlTestCase 不同。