Forum Discussion

pocayoke's avatar
pocayoke
New Contributor
10 years ago

How to simulate an asynchronous web service with mock services in soapUI

I'm trying to simulate an asynchronous service. The service must send an ack response and then after some time invoke a callback service.

 

I have been trying different scripts based on some post thai I have found using testcase to invoke the callback service, but I haven't been able to get that the service sends the response before the invokation of the callback service.

 

Does it exits a way to run a test case and then send a response without wait that the test case finishes?

 

Thank you very much

  • Hi,

     

    Yes, this can be done effectively by:

     

    1. setup a mock to handle the initial request.
    2. When this mock is called, use Groovy script to call a TestCase asynchnously: 

    (this example was for a quote service, these are parameters to pass to the call back TestCase)

     

    def map = new StringToObjectMap() 
    map.put("messageID", requestMessageId)
    map.put("quoteId", "12345")
    map.put("amount", "777")

    def testSuite = context.mockService.project.
    getTestSuiteByName("TestSuite - Async Call & Callback")
    def callBackTestCase = testSuite.getTestCaseByName("Callback
    TestCase")

    def callBackRequest = (WsdlTestRequestStep) callBackTestCase.getTe
    stStepsOfType(WsdlTestRequestStep.class).get(0)

    callBackRequest.testRequest.setEndpoint(replyTo)
    callBackTestCase.run(map, true) //true here means call asynchronously i.e. dont wait to the TestCase to finish first

     

    3. The mock script should finish and you can add a suitable ack response to the mock.

    4. In the TestCase that is called, you can then add a Request TestStep to simulate your call back. You can add a delay TestStep before the request TestStep if you want more delay for the callback.

     

    Please let me know if you need more explanation of this. There is a SOAP worked example of this approach from my SoapUI Cookbook, think this is part of a free sample chapter 4 (recipe: Testing asynchronous SOAP service callbacks)

     

    Hope this helps,

    Cheers,

    Rupert

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi,

     

    Yes, this can be done effectively by:

     

    1. setup a mock to handle the initial request.
    2. When this mock is called, use Groovy script to call a TestCase asynchnously: 

    (this example was for a quote service, these are parameters to pass to the call back TestCase)

     

    def map = new StringToObjectMap() 
    map.put("messageID", requestMessageId)
    map.put("quoteId", "12345")
    map.put("amount", "777")

    def testSuite = context.mockService.project.
    getTestSuiteByName("TestSuite - Async Call & Callback")
    def callBackTestCase = testSuite.getTestCaseByName("Callback
    TestCase")

    def callBackRequest = (WsdlTestRequestStep) callBackTestCase.getTe
    stStepsOfType(WsdlTestRequestStep.class).get(0)

    callBackRequest.testRequest.setEndpoint(replyTo)
    callBackTestCase.run(map, true) //true here means call asynchronously i.e. dont wait to the TestCase to finish first

     

    3. The mock script should finish and you can add a suitable ack response to the mock.

    4. In the TestCase that is called, you can then add a Request TestStep to simulate your call back. You can add a delay TestStep before the request TestStep if you want more delay for the callback.

     

    Please let me know if you need more explanation of this. There is a SOAP worked example of this approach from my SoapUI Cookbook, think this is part of a free sample chapter 4 (recipe: Testing asynchronous SOAP service callbacks)

     

    Hope this helps,

    Cheers,

    Rupert

    • vasu854's avatar
      vasu854
      New Member

      Hi,

      Can you please provide with exaample for the above steps, I am looking for similar situation.

       

      in my case, perl code call my mock service url and soapui sends mock response to the caller and few seconds later need to send one more response back to the caller.

       

      how to do this using groovy script, and test case/step having the final response ?

    • pocayoke's avatar
      pocayoke
      New Contributor

      Thank you very much for your message, it was very useful