Forum Discussion

jamescollett's avatar
jamescollett
Contributor
10 years ago

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.
  • jamescollett's avatar
    jamescollett
    10 years ago

    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.

     

     

  •  

    A possible workaround could 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    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

    Check the details in the post here: http://stackoverflow.com/questions/32975240/soapui-mock-service-custom-sequence-of-responses/32980121#32980121 

    • jamescollett's avatar
      jamescollett
      Contributor
      Albert,

      AlbertCiffone wrote:

       

      A possible workaround could 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
      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

      Check the details in the post here: http://stackoverflow.com/questions/32975240/soapui-mock-service-custom-sequence-of-responses/32980121#32980121 


      Thanks. This is interesting and I think I get what you are doing here. It doesn't work, though.

       

      Interestingly the log.info statement does output the names of the responses I want, for example, if I specify my list to be just two of the responses out the 5 that I have currently defined, those two are output in the script log:-

       

       

       

          myRespList = ["Response 2","Response 4"] 
      ...
      ...
      ...
      ...
      // return the response
      log.info "-->"+resp
      return resp

      This produces in the script log the following:-

       

      Wed Oct 07 12:28:00 BST 2015:INFO:-->[Response 2]
      Wed Oct 07 12:28:01 BST 2015:INFO:-->[Response 4]
      Wed Oct 07 12:28:03 BST 2015:INFO:-->[Response 2]
      Wed Oct 07 12:28:04 BST 2015:INFO:-->[Response 4]
      Wed Oct 07 12:28:04 BST 2015:INFO:-->[Response 2]
      Wed Oct 07 12:28:06 BST 2015:INFO:-->[Response 4]

       

      and so on.

       

      But the response that comes back to the client (which is another instance of SoapUI running as a client on another workstation) is whatever is specified in the Deafult Response dropdown list on the server instance.

       

      It's like the "return" statement in the script is being ignored by the tool.

       

      • AlbertCiffone's avatar
        AlbertCiffone
        New Contributor

        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.

  • Really, does no-one have any wisdom to offer on this one? This sort of mock service setup has to be in the category of Mock Service Scripting 101 and yet no advice on how to make it work?

     

    Sigh.

     

    • jamescollett's avatar
      jamescollett
      Contributor

      I am re-visiting this problem once again. This time I have moved up to SoapUI 5.2.0. And this time I am modelling a RESTful web service instead of a SOAP web service.

       

      Again I should like to define a specific order of responses from the set of responses that I have created for a method. If I use the SEQUENCE option, the responses will come out in the order in which they were added. But I do not want this. Suppose I want to just return the 4th and the 5th response and for now ignore the 1st, 2nd and 3rd?

       

      The tool does not allow me to arbitrarily move the responses as they are listed. I do not wish to delete any of them. I should like to write a script that says, "Send back the 4th response, then the 5th, regardless of the contents of the incoming request."

       

      Please, anyone?

      • rupert_anderson's avatar
        rupert_anderson
        Valued Contributor

        Hi,

         

        To do what you need and many other dynamic responses, I would use a Dispatch type of SCRIPT. Please see example in the screen shot:

         

        Screen Shot 2015-10-06 at 17.23.58.png

         

        Basically, the script allows you to conditionally return different responses e.g. you could do return "response 4" or whatever you need.

         

        Assuming you're happy to do some simple Groovy script, does this make sense? If you're not happy to, then your options are limited to the sequence option.

         

        Hope this helps,

        Cheers,

        Rupert