我正在尝试在 groovy 中比较两个数组。到目前为止,我的尝试产生了不同的反应,因此我正在向集体寻求建议。
在下面的代码中,我获取 2 个 REST 响应,解析它们并将 Invoice 节点下的所有内容放入一个数组中。然后我进一步限定了我的数组,因此我有一个 InvoiceID 列表,然后尝试比较两个响应的结果以确保它们相同。
当我比较它们匹配的 InvoiceIDs (Guids) 数组时 - 这不是我所期望的,因为我的 2 个响应源之间的发票顺序当前不同。
当我对发票 ID 数组进行排序时,结果会有所不同。
我怀疑我的代码有问题,但是花了一个小时来敲打它,但无济于事。
任何有关在 groovy 中或以下代码中排序数组的建议将不胜感激:
gu = new com.eviware.soapui.support.GroovyUtils( context )
def xmlSlurper = new groovy.util.XmlSlurper()
// Setting up the response parameters
def responseSTAGE = xmlSlurper.parseText(context.expand('${GET Invoices - STAGE#Response}'));
def responseSTAGE2 = xmlSlurper.parseText(context.expand('${GET Invoices - STAGE2#Response}'));
responseInvoicesSTAGE = responseSTAGE.Invoices
responseInvoicesSTAGE2 = responseSTAGE2.Invoices
def arrayOfInvoicesSTAGE = []
def arrayOfInvoicesSTAGE2 = []
def counter = 0
for (invoice in responseInvoicesSTAGE.Invoice) {
arrayOfInvoicesSTAGE[counter] = responseInvoicesSTAGE.Invoice[counter].InvoiceID
//log.info counter+" STAGE"+arrayOfInvoicesSTAGE[counter]
arrayOfInvoicesSTAGE2[counter] = responseInvoicesSTAGE2.Invoice[counter].InvoiceID
//log.info counter+" STAGE2"+arrayOfInvoicesSTAGE2[counter]
counter++
}
log.info arrayOfInvoicesSTAGE
log.info arrayOfInvoicesSTAGE2
def sortedSTAGE = arrayOfInvoicesSTAGE.sort()
def sortedSTAGE2 = arrayOfInvoicesSTAGE2.sort()
log.info sortedSTAGE