2014-11-17 50 views
0

在SoapUI(和SOAP Web服務)中,如何評估請求中的字符串並根據請求的條件返回響應?SoapUI:使用Groovy根據請求的驗證返回MockResponse

到目前爲止,我能夠讀取請求中的值並在響應中使用這些值,但是如果嘗試將此擴展爲基於if語句的響應,則不會返回任何值。任何幫助,將不勝感激。這是我到目前爲止的代碼:

// create XmlHolder for request content 
def holder = new com.eviware.soapui.support.XmlHolder(mockRequest.requestContent) 

// get arguments from the request (this looks like it is all working correctly) 
def FirstName = holder.getNodeValue("//FirstName") 
def LastName = holder.getNodeValue("//LastName") 
def Phone = holder.getNodeValue("//Phone") 
def Gender = holder.getNodeValue("//Gender") 
def Age = holder.getNodeValue("//Age") 

//here are where the problems start 
//I am using an evaluation in the if statement that should always be true 
//I was trying something closer to the end solution I would like eg. if(Gender == "M") 
//but this also failed to return any response 
def response 
if("M" == ("M")){ 
    reponse = FirstName; 
    //return response 
    //context.setProperty("responseMessage", response) 
}else 
{ 
    response = "error" 
    //return response 
    //context.setProperty("responseMessage", response) 
} 
    return response 

context.setProperty("responseMessage", response) 

我已經留在作爲註釋一些我曾嘗試

感謝

編輯其他的東西:這是soapUI的MockResponse內,我要定${responseMessage}MockReponse作爲XML中的響應值。我的目標是向模擬服務發送請求,並能夠根據請求中包含的字符串進行響應,例如「性別」是「M」還是「F」,年齡是否在某個範圍內等等。我覺得如果我在這個問題的代碼問題解決了,這樣返回的值,那麼我就可以解決擴展部分自己

由於albciff,這解決了我的問題:

... 
if("M" == ("M")) 
{ 
    requestContext.responseMessage = FirstName 
}else 
{ 
    requestContext.responseMessage ="error" 
} 
+0

我不知道如果我理解你的要求...你想要做什麼?在groovy的肥皂測試步驟中寫回應?或者相反,你有一個mockService,你把這個Groovy代碼放在'AfterRequest Script'中? – albciff

+0

我試圖添加這個作爲編輯的問題,但它被拒絕了:這是在SoapUI響應中,將$ {responseMessage}設置爲XML中的響應值之後。我的目標是向模擬服務發送請求,並根據請求中包含的字符串進行響應,例如「性別」是「M」還是「F」,年齡在一定範圍內等。我覺得如果我的問題與問題中的代碼已解決,以便返回一個值,那麼我可以自己解決擴展部分 – Deminish

回答

0

我猜想你有以下情況。

你創造了一個SOAPUI MockService,裏面MochService有一個operation具有MockResponse和你發送從operation爲腳本的響應。在後續的圖像MockServiceoperationMockResponse顯示:

enter image description here

所以,舉例來說,如果你有一個後續的內容MockResponse

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body>${responseMessage}</soap:Body> 
</soap:Envelope> 

在你的腳本來填補${responseMessage}你有通過requestContext對象來指定屬性值,而不是在context中設置屬性或返回值,在這種情況下爲requestContext.responseMesssage

在腳本中,這可能是:

... 
if(Gender == "M"){ 
    requestContext.responseMesssage = FirstName 
}else 
{ 
    requestContext.responseMesssage = "error" 
} 
... 

您還可以看看SOAPUI MockService documentation

希望這有助於

+0

非常感謝,這已成功竅門 – Deminish

+0

不客氣':'' – albciff