2

我在我的 SoapUI 中执行以下 Groovy 脚本

import com.* 
import javax.xml.XMLConstants
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.SchemaFactory

def xsd = '''
<xs:schema targetNamespace="http://maps.googleapis.com" elementFormDefault="qualified" xmlns="http://maps.googleapis.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="GeocodeResponse" type="_GeocodeResponse"/>
   <xs:complexType name="result_geometry">
      <xs:sequence>
         <xs:element name="location" type="geometry_location"/>
         <xs:element name="location_type" type="xs:string"/>
         <xs:element name="viewport" type="geometry_viewport"/>
         <xs:element name="bounds" type="geometry_bounds"/>
      </xs:sequence>
   </xs:complexType>
   <xs:complexType name="viewport_southwest">
      <xs:sequence>
         <xs:element name="lat" type="xs:decimal"/>
         <xs:element name="lng" type="xs:decimal"/>
      </xs:sequence>
   </xs:complexType>
   <xs:complexType name="bounds_northeast">
      <xs:sequence>
         <xs:element name="lat" type="xs:decimal"/>
         <xs:element name="lng" type="xs:decimal"/>
      </xs:sequence>
   </xs:complexType>
   <xs:complexType name="geometry_viewport">
      <xs:sequence>
         <xs:element name="southwest" type="viewport_southwest"/>
         <xs:element name="northeast" type="viewport_northeast"/>
      </xs:sequence>
   </xs:complexType>
   <xs:complexType name="geometry_bounds">
      <xs:sequence>
         <xs:element name="southwest" type="bounds_southwest"/>
         <xs:element name="northeast" type="bounds_northeast"/>
      </xs:sequence>
   </xs:complexType>
   <xs:complexType name="bounds_southwest">
      <xs:sequence>
         <xs:element name="lat" type="xs:decimal"/>
         <xs:element name="lng" type="xs:decimal"/>
      </xs:sequence>
   </xs:complexType>
   <xs:complexType name="viewport_northeast">
      <xs:sequence>
         <xs:element name="lat" type="xs:decimal"/>
         <xs:element name="lng" type="xs:decimal"/>
      </xs:sequence>
   </xs:complexType>
   <xs:complexType name="_GeocodeResponse">
      <xs:sequence>
         <xs:element name="status" type="xs:string"/>
         <xs:element name="result" type="GeocodeResponse_result"/>
      </xs:sequence>
   </xs:complexType>
   <xs:complexType name="address_component">
      <xs:sequence>
         <xs:element name="long_name" type="xs:string"/>
         <xs:element name="short_name" type="xs:string"/>
         <xs:element name="type" type="xs:string" maxOccurs="2"/>
      </xs:sequence>
   </xs:complexType>
   <xs:complexType name="GeocodeResponse_result">
      <xs:sequence>
         <xs:element name="type" type="xs:string" maxOccurs="2"/>
         <xs:element name="formatted_address" type="xs:string"/>
         <xs:element name="address_component" type="address_component" maxOccurs="3"/>
         <xs:element name="geometry" type="result_geometry"/>
      </xs:sequence>
   </xs:complexType>
   <xs:complexType name="geometry_location">
      <xs:sequence>
         <xs:element name="lat" type="xs:decimal"/>
         <xs:element name="lng" type="xs:decimal"/>
      </xs:sequence>
   </xs:complexType>
</xs:schema>
'''.trim()

def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
def schema = factory.newSchema(new StreamSource(new StringReader(xsd)))
def validator = schema.newValidator()
validator.validate(new StreamSource(new StringReader(XmlExamples.G_RECORDS)))

XmlExamples.groovy 位于C:\Program Files\SmartBear\SoapUI-4.6.1\bin\ext\com

IE

class XmlExamples {
  static def G_RECORDS = '''
   <GeocodeResponse>
   <status>OK</status>
   .....

         </bounds>
      </geometry>
   </result>
</GeocodeResponse>
  '''
}

但问题是我在 SoapUI 中遇到以下错误

:ERROR:groovy.lang.MissingPropertyException: No such property: XmlExamples for class: Script1
   groovy.lang.MissingPropertyException: No such property: XmlExamples for class: Script1
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
    at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
    at Script1.run(Script1.groovy:86)
    at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:96)
    at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:149)
    at com.eviware.soapui.impl.wsdl.support.AbstractTestCaseRunner.runTestStep(AbstractTestCaseRunner.java:239)
    at com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner.runCurrentTestStep(WsdlTestCaseRunner.java:48)
    at com.eviware.soapui.impl.wsdl.support.AbstractTestCaseRunner.internalRun(AbstractTestCaseRunner.java:148)
    at com.eviware.soapui.impl.wsdl.support.AbstractTestCaseRunner.internalRun(AbstractTestCaseRunner.java:43)
    at com.eviware.soapui.impl.wsdl.support.AbstractTestRunner.run(AbstractTestRunner.java:135)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

可能的根本原因是什么。与 CLASSPATH 有什么关系吗?我相信默认位置是 .......bin/ext

干杯

4

2 回答 2

0

该类XmlExamples似乎缺少正确的包声明。由于它位于一个名为的包中,因此com您应该具有以下类:

package com

class XmlExamples {
  static def G_RECORDS = '''
    <GeocodeResponse>
      <status>OK</status>
        .....
          </bounds>
        </geometry>
      </result>
    </GeocodeResponse>
  '''
}
于 2013-11-09T21:35:11.807 回答
0

看来您正在尝试使用示例使用 xsd 验证 xml,并且当我尝试在同一个 groovy 脚本中复制的两个代码片段时,相同的示例代码在 SoapUI 工具中成功执行。可能您会想尝试这样来隔离问题。

于 2015-01-16T21:44:19.407 回答