Forum Discussion

apoorva's avatar
apoorva
Occasional Contributor
16 years ago

Replace "?" with default value in MockResponse using Groovy Script

Hi All,

I created MockResponse for one of the operation in MockService.

By default, Default value for all elements in MockResponse is "?".
Now I want to insert some value instead of "?" in MockResponse using groovy script...

My MockResponse is looks like below


 
     
       
           
              ?             
              ?
              ?

             
                               
                    ?
                    ?
                                       
                            ?
                            ?                   
                                               
                   

                    ?                                   
                    ?
                   
             

     
           

           
              ?
              ?
           

            ?
       

     

 



I will appreciate if any input will be available.

Regards,
Jim

3 Replies

  • omatzura's avatar
    omatzura
    Super Contributor
    Hi Jim,

    you can use the Response Script (at the bottom of the MockResponse editor) to dynamically insert values into a response, just set the desired values in the context and then expand them in your response.. for example this script

    context.randomNum = Math.random()

    and this expansion in your mockresponse xml

    ${randomNum}

    will return a mock response with a random number in the someElement..

    Hope this helps!

    regards,

    /Ole
    eviware.com
  • apoorva's avatar
    apoorva
    Occasional Contributor
    Hi Ole,

    Thanks for your reply... But I want to replace with specific value, not random one and  I write following script to do this

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    def holder = groovyUtils.getXmlHolder( "Request 1#Response" )

    holder.namespaces["ns"] = "http://sidmgmt.com/xsd"
    def res = holder.getNodeValue( "//ns:results" )

    def ret = groovyUtils.getXmlHolder(res)
    ret.namespaces["ns1"] = "http://service.bds.com/xsd"
    def codevalue = ret.getNodeValue( "//ns1:code" )
    ret.setNodeValue("//ns1:code", "400") //not sure

    I got following error
    com.eviware.soapui.impl.wsdl.mock.DispatchException: Failed to dispatch using script; java.lang.NullPointerException

    Can you please suggest whats the suggested argument value in getXmlHolder to get access to mockResponse..


    Thanks,
    Jim
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi Jim

    maybe you could just put


    ${code}

    into your response and then set

    context.code = "400"

    in the response script?

    regards,

    /Ole
    eviware.com