SOAPUI,即使json数据正确,JsonSlurper也会返回null

时间:2013-11-08 07:16:45

标签: groovy soapui

在SOAPUI中,即使json数据正确,JsonSlurper也会返回null。以下jsonObj给出null。是因为冗长的数据还是其他一些东西。我验证了我的数据格式及其正确性。

import groovy.json.JsonSlurper
def jsonObj = new JsonSlurper().parseText( messageExchange.response.contentAsString )

1 个答案:

答案 0 :(得分:0)

好的,所以不要去。这可能是由于在Java 6上运行SoapUI(它不能与Java 7一起运行)。

因此,解决方法。

互联网上的示例并不一定有用,因为:

  • 他们可能是由不太熟悉Groovy的人写的
  • 这些例子隐藏了一个基本问题:从SoapUI获得的对象似乎在不同的上下文中表现不同,例如:在SOAP或REST上下文中,对象的实际类是不同的,虽然可以执行相同的方法调用,但返回的值将不同(您获得的是XML字符串而不是JSON字符串)。由于Groovy是弱类型并鼓励鸭子打字,所给出的例子可能会以意想不到的方式失败,而不是立即明显的方式。
  • 示例缺少日志记录和断言。

这些说,我们走了。我们想获得可以在响应中找到的“InquiryId”的值,我们知道它由JSON字符串表示。

// Get projet associated to current "testRunner" by walking up the tree of objects    
// "testRunner" is probably a http://www.soapui.org/apidocs/com/eviware/soapui/impl/wsdl/support/AbstractTestCaseRunner.html    

def myProject = testRunner.testCase.testSuite.project

// "myProject" is probably a http://www.soapui.org/apidocs/com/eviware/soapui/impl/wsdl/WsdlProject.html 
// Let's verify this! If we have guessed wrongly, the script will fail

assert myProject instanceof com.eviware.soapui.impl.wsdl.WsdlProject

// Get the appropriate TestStep by walking down the tree of objects
// It is recommended to use getXByName() throughout unless you want to 
// confuse yourself by using mixed accessor styles working only on some
// classes that may appear in this expression:

def myStep = myProject.getTestSuiteByName("Global test suite").getTestCaseByName("Test Case Foo").getTestStepByName("Create Inquiry")

// The result is probably a http://www.soapui.org/apidocs/com/eviware/soapui/impl/wsdl/teststeps/WsdlTestStep.html
// Let's verify this! If we have guessed wrongly, the script will fail

assert myStep instanceof com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep

// Getting JSON string can be done like this. One can also do
// 'otherStep.getPropertyValue("reponse") as String' 

def jsonString = otherStep.testRequest.response.contentAsString

// Let's log what we got here:     

log.info "Returned JSON string is '${jsonString}'"

// What we cannot do with the 'jsonString' is:
// Give it to SoapUI's "XMLHolder" for parsing, because, in spite of people
// saying so, it does not seem to understand JSON.
// Give it to JsonSlurper because JsonSlurper returns null
// So we extract the value by scanning the String directly using a regular 
// expression. See http://groovy.codehaus.org/Regular+Expressions
// The pattern say: Look for something that starts with 
// {"InquiryId":"
// and ends with
// "}
// and grab the characters between these endings.
// We then look for "anywhere in the passed String" for the above using "find()"
// rather than "match()" which would demand precise matching of the whole String.

def pattern = ~/\{"InquiryId":"(.+?)"\}/    
def matcher = pattern.matcher(jsonString)

if (matcher.find()) {    
   def inqId = matcher.group(1)    
   log.info "Returned string is '${inqId}'"
   // We are done here
   return inqId    
}    
else {    
   throw new IllegalArgumentException("Could not extract InquiryId from '$jsonString'")    
}