Forum Discussion

tpowers's avatar
tpowers
Occasional Contributor
15 years ago

Help with Mock Responses

Hi All;

I have been using SoapUI for a bit over a year now, purely as a client to file/retrieve data to/from a db. I was simply editing/copying requests to manually build test suites.

I now am charged with testing a WS client from my db against non existent services. I am able to manually mock up the responses, but now need to get a bit more dynamic in my responses.

I need to able to do 2 things....

1. Read a portion of the request (min 2 parameters, more in some cases) and use that to determine the mock response dispatch.
2. Return a portion of each request exactly as submitted.

Based on a little lurking here, and reading the help doc, I am thinking that scripting would be the best way to achieve both objectives...

Might someone be able to provide a simple example of each? Or possibly point to a tutorial(s). My presumption is that the dispatch scripting will take place in the 'Mock Operation Editor' and the defaulting behavior from request data will be scripted in the 'Mock Response Editor'

I currently am using the free 3.0.1.
I have very little coding experience, no groovy experience; but can alter once I get a simple working version of each.

Thanks in advance,
-Tim

4 Replies

  • tpowers's avatar
    tpowers
    Occasional Contributor
    I am working on the MockResponse dispatch and I think I have it right so far...


    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
    def holder = groovyUtils.getXmlHolder( mockRequest.requestContent )
    def scriptName = holder.getNodeValue( "//scriptName")
    def optionId = holder.getNodeValue( "//OptionId")

    if (scriptName == "TEST2")



    How do I now specify the MockResponse to use? And if i were to use multiple arguments in my if statement, what is the groovy syntax?

    I have searched the forum high and low.

    Thanks again...
    -Tim
  • tpowers's avatar
    tpowers
    Occasional Contributor
    Ah yes, I had looked a that help topic, however I did not notice that "Invalid Session Id Fault" was one of the defined responses, since it is cut off in the screen shot....

    That does help! I can now execute a dispatch, sort of.

    I did not have success with the statement however. I do not believe I am obtaining values from the request properly...

    My new code..
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
    def holder = groovyUtils.getXmlHolder( mockRequest.requestContent )
    def scriptName = holder.getNodeValue( "//scriptName")
    def optionId = holder.getNodeValue( "//OptionId")

    if (scriptName == "TEST2")
    return "Response2"
    else return "error"


    always returns the "error" response, so at least I know it is being executed!

    They are nested within objects, do I need to add their hierarchy?
    some thing like... (//RunScript/scriptName)

    The request is... (scriptName and OpionId tags near bottom)
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
      <soapenv:Header/>
      <soapenv:Body>
          <tem:RunScript>
            <!--Optional:-->
            <tem:optionObject>
                <!--Optional:-->
                <tem:ErrorMesg> </tem:ErrorMesg>
                <tem:ErrorCode> </tem:ErrorCode>
                <!--Optional:-->
                <tem:Forms>
                  <!--Zero or more repetitions:-->
                  <tem:FormObject>
                      <!--Optional:-->
                      <tem:CurrentRow>
                        <!--Optional:-->
                        <tem:Fields>
                            <!--Zero or more repetitions:-->
                            <tem:FieldObject>
                              <!--Optional:-->
                              <tem:FieldNumber>FIELD1</tem:FieldNumber>
                              <!--Optional:-->
                              <tem:FieldValue>XXXXXXXX</tem:FieldValue>
                              <!--Optional:-->
                              <tem:Required>0</tem:Required>
                              <!--Optional:-->
                              <tem:Enabled>1</tem:Enabled>
                              <!--Optional:-->
                              <tem:Lock> </tem:Lock>
                            </tem:FieldObject>
                        </tem:Fields>
                        <!--Optional:-->
                        <tem:ParentRowId>0</tem:ParentRowId>
                        <!--Optional:-->
                        <tem:RowId>ROW1</tem:RowId>
                      </tem:CurrentRow>
                      <!--Optional:-->
                      <tem:FormId>FORMID1</tem:FormId>
                      <tem:MultipleIteration>0</tem:MultipleIteration>
                      <!--Optional:-->
                      <tem:OtherRows>
                        <!--Zero or more repetitions:-->
                        <tem:RowObject>
                            <!--Optional:-->
                            <tem:Fields>
                              <!--Zero or more repetitions:-->
                              <tem:FieldObject>
                                  <!--Optional:-->
                                  <tem:FieldNumber> </tem:FieldNumber>
                                  <!--Optional:-->
                                  <tem:FieldValue> </tem:FieldValue>
                                  <!--Optional:-->
                                  <tem:Required> </tem:Required>
                                  <!--Optional:-->
                                  <tem:Enabled> </tem:Enabled>
                                  <!--Optional:-->
                                  <tem:Lock> </tem:Lock>
                              </tem:FieldObject>
                            </tem:Fields>
                            <!--Optional:-->
                            <tem:ParentRowId> </tem:ParentRowId>
                            <!--Optional:-->
                            <tem:RowId> </tem:RowId>
                        </tem:RowObject>
                      </tem:OtherRows>
                  </tem:FormObject>
                </tem:Forms>
                <!--Optional:-->
                <tem:OptionId>OPTION2</tem:OptionId>
                <!--Optional:-->
                <tem:SystemCode>SYSTEM1</tem:SystemCode>
                <!--Optional:-->
                <tem:EntityID>OOOOOOO</tem:EntityID>
                <!--Optional:-->
                <tem:EpisodeNumber> </tem:EpisodeNumber>
                <tem:Facility>QQQQ</tem:Facility>
                <!--Optional:-->
                <tem:OptionUserId>ZZZZZZ</tem:OptionUserId>
                <!--Optional:-->
                <tem:OptionStaffId>YYYYYYY</tem:OptionStaffId>
            </tem:optionObject>
            <!--Optional:-->
            <tem:scriptName>TEST2</tem:scriptName>
          </tem:RunScript>
      </soapenv:Body>
    </soapenv:Envelope>



    Do scriptName and OptionId also need to be defined as properties somehwere? I figured i would have to do that in the MockResponse when it came time to modify the response, but I thought that would have been separate from the dispatch (I would have to read in from the xmlHolder again in the repsonse)

    More thanks...
    -Tim
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    You have namespace declarations so I think you would have to do

    def scriptName = holder.getNodeValue( "//tem:scriptName")


    If you do

    log.info scriptName


    after you get the value from the XmlHolder you can see what it is in the script log.