2015-10-06 111 views
0

我有一個帶有多個響應的SoapUI模擬服務。我想爲我的回答定義一個自定義序列,並且我不一定在這個特定的測試中使用所有的回答。SoapUI模擬服務自定義響應順序

我實際上設法讓它在過去工作,但我認爲產品的更新版本中某些內容發生了變化,並且該功能停止工作。這是一個SOAP Web服務。現在我正在嘲笑一個RESTful Web服務,並且我有相同的要求來幫助我完成我的測試。

SEQUENCE調度選項不是我想要的,因爲它會按照創建順序返回所有已定義的響應。 SCRIPT選項是我之前使用的,但現在我可以用這個選項來定義一個響應來生成。對於這個測試,我沒有興趣檢查請求的某些內容以決定發回哪個響應。

舉例來說,如果我有8個迴應定義,我只是希望能夠指定以下響應返回: -

響應#2,然後回覆#3,然後回覆#4,最後迴應#7;以便不使用響應#1,#5,#6和#8。

我的問題提出了詳細的SmartBear論壇在這裏: - simple scripting in a Mock Service - no longer works

+0

@albciff - 這是一個有趣的問題。你的解決方案沒有回答我的問題;它並沒有真正的工作。 (請仔細閱讀並查看我在列表中分配字符串的方式時發現的奇怪之處)。然而,這非常有幫助,並構成了我解決方案的中堅力量。 – jamescollett

+0

@albciff親愛的同事,我應該很樂意爲您提供榮譽,但我也注意到您的解決方案完全如所提供的那樣存在缺陷。方括號不是詞典風格的選擇;由於Groovy字符串操作的奇怪效果,它們是必需的。沒有它們,它不起作用。至少對我來說這是奇怪的。我以爲我明確表示了。由於除了接受它作爲解決方案外,沒有辦法爲您的貢獻付款,所以我會做這件事,但其他讀者注意到必要的改動。我希望能減輕你的煩惱。 – jamescollett

+0

我不在乎答案是否被接受......既不是關於榮譽(它是什麼意思?)我對你的評論感到憤怒......可能我過度反應,但答案對我有用,我感到有點生氣(太多了)......我想爲我的反應道歉......當然,如果你覺得這確實不能解決你的問題,那麼你可以自由地接受這個問題。 – albciff

回答

1

我儘可能嘗試使用連續的語句返回與響應順序在SOAPUI論壇張貼這是行不通的。

而不是你的groovy代碼作爲一個調度腳本我的目的是使用以下groovy代碼作爲解決方法,它包括使用一個列表來保持您的響應按期望的順序,並保持這個列表在context更新它使用下面的代碼每次:

// get the list from the context 
def myRespList = context.myRespList 

// if list is null or empty reinitalize it 
if(!myRespList || !myRespList?.size){ 
    // list in the desired output order using the response names that your 
    // create in your mockservice 
    myRespList = ["Response 2","Response 3","Response 4","Response 7"] 
} 
// take the first element from the list 
def resp = myRespList.take(1) 
// update the context with the list without this element 
context.myRespList = myRespList.drop(1) 
// return the response 
log.info "-->"+resp 
return resp 

此代碼的工作像您期望的,因爲context是保持清單,每次該腳本返回下一個響應,當列表爲空再次重新填充它的重新啓動循環以相同的順序。

,當我用這個mockService我得到後續的腳本日誌的說明:

enter image description here

編輯

如果爲OP您與您的SOAPUI版本,因爲返回的字符串問題在方括號之間,如:[Response 1],使用以下方法更改元素從陣列中獲取的方式:

// take the first element from the list 
def resp = myRespList.take(1)[0] 

代替:

// take the first element from the list 
def resp = myRespList.take(1) 

注意[0]

通過此更改,返回字符串將是Response 1而不是[Response 1]

在這種情況下,腳本將是:

// get the list from the context 
def myRespList = context.myRespList 

// if list is null or empty reinitalize it 
if(!myRespList || !myRespList?.size){ 
    // list in the desired output order using the response names that your 
    // create in your mockservice 
    myRespList = ["Response 2","Response 3","Response 4","Response 7"] 
} 
// take the first element from the list 
def resp = myRespList.take(1)[0] 
// update the context with the list without this element 
context.myRespList = myRespList.drop(1) 
// return the response 
log.info "-->"+resp 
return resp 

希望這有助於

相關問題