Forum Discussion

jamescollett's avatar
jamescollett
Contributor
10 years ago
Solved

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 ...
  • jamescollett's avatar
    jamescollett
    9 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.