2013-01-16 117 views
10

如何從外部文件包含groovy腳本? enter image description hereSOAPUI:如何從外部文件包含Groovy腳本

我試着使用:

def script = new GroovyScriptEngine('d:/soapui/payment.v2').with { 
    loadScriptByName('proxy.groovy') 
} 
this.metaClass.mixin script 

,但我得到:

enter image description here

更新

是否存在某種可能收拾我的方法到罐子或類似的東西,並從使用它們?如果你使用ExpandoMetaClass.enableGlobally()然後this.metaClass = null

evaluate(new File('path/to/external.groovy')); 

回答

14

最簡單的方法是使用內SOAPUI

Groovy的測試步驟
import groovy.lang.Binding 
import groovy.util.GroovyScriptEngine 

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) 

// location of script file is relative to SOAPUI project file. 
String scriptPath = groovyUtils.projectPath + "/groovy/" 

// Create Groovy Script Engine to run the script. 
GroovyScriptEngine gse = new GroovyScriptEngine(scriptPath) 

// Load the Groovy Script file 
externalScript = gse.loadScriptByName("Utility.groovy") 

// Create a runtime instance of script 
instance = externalScript.newInstance() 

// Sanity check 
assert instance!= null 

// run the foo method in the external script 
instance.foo() 
+0

有點晚了,但是當你將項目導出爲WAR時會發生?你需要在服務器上的某個地方使用groovy /目錄嗎? – Dave

+0

我還沒有嘗試過,但如果它相對於該項目舉行,它應該工作正常。 –

1

試試這個:

GroovyShell gs = new GroovyShell(getBinding()); 
gs.evaluate(new File('path/to/external.groovy').text); 

甚至這一點。在開始的時候,之前你在開始混合。

+0

我得到了'java.lang.Exception無法獲取屬性'requestContext'對null對象'消息。在發佈之前你檢查過他們嗎? – CAMOBAP

+0

檢查外部文件中的代碼。 Groovy引擎運行它並得到了異常。用簡單的命令替換外部文件的內容,看看它是如何工作的:log.info「來自外部文件的Hello」 –

+0

當我將外部文件的內容放入Script TextArea時,它的工作非常完美。每次嘗試訪問'log','context,'requestContext','mockRequest'和'mockResponse'時都會出現問題變量 – CAMOBAP

0

對於混入錯誤消息,則可能擺脫指出:

2

您還可以在Java中創建腳本(蝕),然後導出它運行作爲一個jar並添加到soapui中。

下面是步驟:

  1. 包內創建Java類代碼。
  2. 在包裝和出口右鍵單擊(選擇JAR)
  3. 加入這個罐子的soapUI /斌/ ext文件夾(確保這一步之前關閉的soapUI)
  4. 重啓soapUI的,現在你可以導入和使用在soapui裏面的腳本如下。

  5. 創建一個常規步驟和導入罐

    導入包name.class名稱

  6. 調用該函數爲:

    類name.function名稱(參數);