Ask a Question

simple scripting in a Mock Service - no longer works

SOLVED
jamescollett
Contributor

simple scripting in a Mock Service - no longer works

I have a problem with Mock Service scripting. It used to work. Now it doesn't.

In a previous project (about a year ago) I used simple scripting in a soapUI Mock Service.

I had several responses defined for a method. For one of my tests, I wanted to ensure that a subset of the responses would be returned in a specific sequence when the method was called repeatedly by the client piece. There was nothing fancy about this - no conditional programming, just a specification of which named responses I wanted to be sent back to the client.

I have had to re-visit this project to fix a small bug and I wanted to do a similar test. However the scripting no longer works.

Effectively I have the following responses in the Mock Responses panel of my Login method:-

Login_Response_OKAY_1
Login_Response_OKAY_2
Login_Response_OKAY_3
Login_Response_OKAY_4
Login_Response_ERROR


In the Dispatch dropdown list I have "SCRIPT" selected. In the Default Response dropdown list I have "Login_Response_ERROR" selected. In the panel below this I have the following script:-

return "Login_Response_OKAY_2"
return "Login_Response_OKAY_1"
return "Login_Response_OKAY_3"
return "Login_Response_OKAY_4"


In this test I did not want the fifth variant of the response (the error case) sent back. This used to work a treat.
I used to get the responses in the order as written, and then they would repeat - i.e. 2, 1, 3, 4, 2, 1, 3, 4 etc).

Now I just get the first response in the list, namely Login_Response_OKAY_2, repeatedly.

I am using soapUI 5.0.0 and I have Groovy specified as the scripting language (I think this is the default).

The only difference I can think of between now and a year ago is that I was probably using an earlier version of soapUI.

Can someone suggest what is wrong and how to cure this?

Thanks.
13 REPLIES 13


@AlbertCiffone wrote:

It works correctly for me. Check that the string that you're returning correspond to the response names... you use "Login_Response_OKAY_1" as name instead of "Response 1"... In my example I use this names but check that you're using the correct ones for your case.

 

...
myRespList
= ["Login_Response_OKAY_1","Login_Response_OKAY_2"]
...

If you return a string that not exist as a name it executes the default option.


Thanks, Albert. The names have changed across different projects and iterations, and my post is meant to convey simply an example of what is happening. But yes, I have made sure in each case that the name I quote matches the name of a Response.

 

I tried renaming the Responses to have an underscore character instead of a space character. In the old days, putting a space in a filename was a sackable offence (joke) because it generated problems on the file system. But this did not change a thing.

 

I remain clueless as to what foolish error I have made.

 

I have found a solution. I have moved on to a new SoapUI project (a SOAP project, this time, not REST) and just for the sake of punishing myself I tried again to construct a custom sequence. I have taken bits of a coupe of the kind responses and amended them based on my observations and have come up with the following.

 

I realised that the script log was showing the values of my list items with the square brackets around them. It occurred to me that a statement like this:-

 

 myRespList = ["Response_2","Response_1"]  

- must do a bit more than just insert the two strings "Response_2" and "Response_1" to a list; it must be adding the square brackets around the words first. No idea why.

 

I re-named my methods to include the square brackets and suddenly the thing is working.

 

Here are the details:-

 

My Responses in the method are as follows:-

 

[Response_Template]

[Response_1]

[Response_2]

 

My code (heaviliy influenced by previous contributors) is as follows.

 

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

// if list is null or empty reinitalize it
if (!myRespList || !myRespList?.size)
{   
    log.info "--> Refreshing the list"
    // list in the desired output order
    myRespList = ["Response_2","Response_1"]  
}

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

// update the context with the list without this element
context.myRespList = myRespList.drop(1)

// return the response
log.info "--> Response is: " + myResp
return myResp;

The output in the script is like this:-

 

  • Fri Oct 09 18:34:47 BST 2015:INFO:--> Refreshing the list
  • Fri Oct 09 18:34:47 BST 2015:INFO:--> Response is: [Response_2]
  • Fri Oct 09 18:34:50 BST 2015:INFO:--> Response is: [Response_1]
  • Fri Oct 09 18:34:51 BST 2015:INFO:--> Refreshing the list
  • Fri Oct 09 18:34:51 BST 2015:INFO:--> Response is: [Response_2]
  • Fri Oct 09 18:34:51 BST 2015:INFO:--> Response is: [Response_1]
  • Fri Oct 09 18:34:52 BST 2015:INFO:--> Refreshing the list
  • Fri Oct 09 18:34:52 BST 2015:INFO:--> Response is: [Response_2]

The SOAP XML Responses from the mock service are swicthing betwen Response_2 and Response_1 as desired.

 

 

Well done James, glad you managed to work that through!

 

Wll done on sharing back your solution too! 🙂

 

Cheers,

Rupert

Author of SoapUI Cookbook

 
cancel
Showing results for 
Search instead for 
Did you mean: