MissingPropertyException:没有这样的属性:Groovy脚本中的错误

时间:2013-11-07 10:36:12

标签: groovy soapui

我在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

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

干杯

2 个答案:

答案 0 :(得分:0)

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

package com

class XmlExamples {
  static def G_RECORDS = '''
    <GeocodeResponse>
      <status>OK</status>
        .....
          </bounds>
        </geometry>
      </result>
    </GeocodeResponse>
  '''
}

答案 1 :(得分:0)

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