1

I have a SoapUI Mock Service with multiple responses. I want to define a custom sequence for my responses and I do not necessarily use all of the responses in this particular test.

I have actually managed to get this to work in the past, but I think something changed in a newer version of the product and the feature stopped working. That was with a SOAP web service. Now I am mocking a RESTful web service and I have the same requirement to help me do my tests.

The SEQUENCE dispatch option is not what I want, because it will return all of the defined responses in the order in which they were created. The SCRIPT option is what I used previously but now all I can achieve with this is define one response to be generated. For this test I have no interest in examining some content of the request to decide which response to send back.

For example, if I have 8 responses defined, I just want to be able to specify that the following responses are returned:-

Response #2, then Response #3, then Response #4, then finally Response #7; so that Responses #1, #5, #6 and #8 are not used.

My question is posed in detail in the SmartBear forum here:- simple scripting in a Mock Service - no longer works

5
  • @albciff - That's an interesting point. Your solution did not answer my question; it did not actually work. (Have a read and see what oddities I found with the way the strings were assigned in the list.) However, it was extremely helpful and formed the backbone of my solution. Oct 10, 2015 at 21:51
  • @albciff Dear fellow, I should be happy to give you kudos but I am also mindful that your solution, exactly as provided, was flawed. The square brackets were not a choice of lexicographical style; they were necessary because of the weird effect of the Groovy string manipulations. Without them it did not work. That was the oddity, to me at least. I thought I made that clear. Since there is no way to pay credit to your contribution other than to accept it as a solution, I shall do that thing, but other readers take note of the necessary alterations. I hope that assuages your irritation. Oct 15, 2015 at 9:02
  • I don't care about the answer was accepted or not... neither about kudos (what it means?) I feel outrageous for your comment... probably I overreaction but the answer works for me and I feel a bit angry (too much)... I want to apologize for my reaction... and of course you're free to unaccept the question if you feel that really doesn't solve your problem.
    – albciff
    Oct 15, 2015 at 9:57
  • I also update my answer for others which can has the same problem as you, I really had to reaction updating my answer at first and not blaming you... an sorry again for my initial behavior.
    – albciff
    Oct 15, 2015 at 10:09
  • en.wiktionary.org/wiki/kudos :) Oct 18, 2015 at 15:14

1 Answer 1

2

I try as you post in the SOAPUI forum using consecutive returns statements with the response order an it doesn't work.

Instead of your groovy code as a DISPATCH script I purpose to use the follow groovy code as workaround, which consist in a use of a list to keep your responses in the desired order and keeping this list in the context an updating it each time using the following code:

// 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

This code works as you expect, since context is keeping the list and each time this script returns the next response and when the list is empty it repopulate it an restart the loop again in the same order.

As an illustration when I use this mockService I get the follow script log:

enter image description here

EDIT

If as OP you've problems with your SOAPUI version because the returned string is between square brackets as ie: [Response 1], change the way that element is taken from the array using:

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

instead of:

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

Note the [0].

With this change the return string will be Response 1 instead of [Response 1].

In this case the script will be:

// 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

Hope this helps,

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.